Re: VBA - if cell value true, offset, check value then delete if criteria met
Are you trying to delete the "LON" row or the Enroute/Avail row?
Re: VBA - if cell value true, offset, check value then delete if criteria met
Are you trying to delete the "LON" row or the Enroute/Avail row?
Re: VBA - if cell value true, offset, check value then delete if criteria met
*Edit to the last post in the "Right" Example of an Or statement
1.Try to avoid using active cells or selecting cells if you can. In your particular case, the code wasn't activating any cells, so the active cell was always the most recent cell selected by you before you ran the macro.
2. If you use "Or" inside an "If" statement, you need to completely restate the argument.
Wrong:
Right
Keep in mind that in the actual code I provided in the previous response, I removed all instances of "activecell"
I hope this helps!
Sincerely,
Max
Re: VBA - if cell value true, offset, check value then delete if criteria met
1.Try to avoid using active cells or selecting cells if you can. In your particular case, the code wasn't activating any cells, so the active cell was always the most recent cell selected by you before you ran the macro.
2. If you use "Or" inside an "If" statement, you need to completely restate the argument.
Wrong:
Right
Keep in mind that in the actual code I provided in the previous response, I removed all instances of "activecell"
I hope this helps!
Sincerely,
Max
Re: VBA - if cell value true, offset, check value then delete if criteria met
Try this:
Sub work_filter()
Dim LR As Long
Application.ScreenUpdating = False
For LR = Range("F" & Rows.Count).End(xlUp).Row To 2 Step -1
If Range("F" & LR).Value = "LON" Then
If Range("F" & LR + 1) = "ENROUTE" Or Range("F" & LR + 1) = "AVAIL" Then
Rows(LR).EntireRow.Delete
End If
End If
Next LR
Application.ScreenUpdating = True
End Sub
Display More
Re: Find Date in Sheet
Attached is the example workbook I'm using with the code
Re: Find Date in Sheet
I can't recreate your problem, the code is working fine on the example workbook I created. Can you attach your workbook for reference?
Re: Find Date in Sheet
I just read your response, try this:
Private Sub CommandButton2_Click()
Dim PrntDt As String 'Print Date
Dim MyDate As Date 'Set the variable to a date so it can be match with other dates in a lookup function
Dim PrntStRng As Range 'Print Start Range
Dim PrntFnRng As String 'Print Finish Range
MsgBox "Print by Day"
'Target Date to Print
TextBox2.Text = Format(TextBox2.Text, "mm/dd/yyyy")
PrntDt = TextBox2.Text
MyDate = TextBox2.Value
MsgBox "Date to Print = " & PrntDt
'Find Target Date on Sheet
'Change PrntDt to MyDate, look ups will only work with matching data types.
Set PrntStRng = Cells.Find(What:=Format(MyDate, "dddd, mm/dd/yyyy"), _
After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
MsgBox "Print Start Range = " & PrntStRng.Address
'Define Print Stop Range
PrntFnRng = Cells(PrntStRng.Row + 10, PrntStRng.Column + 4).Address
MsgBox "Print Finish Range = " & PrntFnRng
'Print Selection
ActiveSheet.Range(PrntStRng.Address & ":" & PrntFnRng).Select
Selection.PrintPreview
Unload Me
End Sub
Display More
Re: Find Date in Sheet
There are 3 reasons that I can think of off the top of my head which could cause this:
1. Your userform is pointing to the wrong spreadsheet
2. The date is not present on the spreadsheet you are evaluating
3. The data type of the date in your spreadsheet is not being recognized as a date (It's being read as a text field)
What is the number format of the date in your spreadsheet (Date/General/Text/etc.), and what does the date actually read within the spreadsheet (6/20/2017, 06/20/2017, June 20 2017, etc). This would be a lot easier to trouble shoot if you were able to provide an example spreadsheet
[sw]*[/sw]
Re: Find Date in Sheet
Try this:
Private Sub CommandButton2_Click()
Dim PrntDt As String 'Print Date
Dim MyDate As Date 'Set the variable to a date so it can be match with other dates in a lookup function
Dim PrntStRng As Range 'Print Start Range
Dim PrntFnRng As String 'Print Finish Range
MsgBox "Print by Day"
'Target Date to Print
TextBox2.Text = Format(TextBox2.Text, "mm/dd/yyyy")
PrntDt = TextBox2.Text
MyDate = TextBox2.Value
MsgBox "Date to Print = " & PrntDt
'Find Target Date on Sheet
'Change PrntDt to MyDate, look ups will only work with matching data types.
Set PrntStRng = Cells.Find(What:=MyDate, _
After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
MsgBox "Print Start Range = " & PrntStRng.Address
'Define Print Stop Range
PrntFnRng = Cells(PrntStRng.Row + 10, PrntStRng.Column + 4).Address
MsgBox "Print Finish Range = " & PrntFnRng
'Print Selection
ActiveSheet.Range(PrntStRng.Address & ":" & PrntFnRng).Select
Selection.PrintPreview
Unload Me
End Sub
Display More
Seems like you had some problems with the date data types matching up in the lookup - For this I created a data variable (instead of string) so excel is storing the date as it's numeric value and not text.
I was also having some issues with accomplishing your goal using the range variables, I changed some stuff around there too.
Let me know if you have any questions.
Sincerely,
Max
Re: WorksheetFunction Sumif with Multiple Columns and Rows - looping through Budget
Hi Jan,
I'm glad you find this site helpful!
I think I understand what you are trying to accomplish. Try this:
Sub Totals()
Dim lrow As Long
Dim i As Long
Dim j As Integer
lrow = Cells(Rows.Count, 2).End(xlUp).Row
For i = 4 To lrow
For j = 11 To 15
If Cells(i, 1) <> "Units" Then
Cells(i, j) = WorksheetFunction.SumIfs(Range(Cells(i, 1), Cells(i, 10)), Range("A3:J3"), Cells(3, j))
Else
Cells(i, j) = Cells(i, j - 5)
End If
Next j
Range("A" & i & ":E" & i).Copy
Range("K" & i & ":O" & i).PasteSpecial Paste:=xlPasteFormats
If Range("l" & i) = 0 Or Len(Range("l" & i)) = 0 Then Range("K" & i & ":O" & i) = ""
Next i
End Sub
Display More
Sincerely,
Max
Re: WorksheetFunction Sumif with Multiple Columns and Rows - looping through Budget
Hello jan.g!
Try this:
Sub Totals()
Dim lrow As Long
Dim i As Long
Dim j As Integer
lrow = Cells(Rows.Count, 2).End(xlUp).Row
For i = 4 To lrow
For j = 11 To 15
If Cells(i, 1) <> "Units" Then
Cells(i, j) = WorksheetFunction.SumIfs(Range(Cells(i, 1), Cells(i, 10)), Range("A3:J3"), Cells(3, j))
Else
Cells(i, j) = Cells(i, j - 5)
End If
Next j
Range("A" & i & ":E" & i).Copy
Range("K" & i & ":O" & i).PasteSpecial Paste:=xlPasteFormats
Next i
End Sub
Display More
I think this is what you are looking for, let me know if you have any questions!
Sincerely,
Max
Edit: Quick change to the code
Re: Copy/Paste Text, then Merge & Left Justify Column Range
Hello! I rewrote most of your code to run more efficient. General rule of thumb when writing VBA: 99% of the time you should be able to complete your task without "Selecting" any objects. Also the trick to find the last row in a data set is very useful and makes your code run a bit faster:
Sub Update()
Dim lrow As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
'Set sheets to a variable so we don't have to type it out again
Set ws1 = Sheets("Update")
Set ws2 = Sheets("Data")
'This is a quick trick to use in order to find the last row. This is much more efficient than running a loop.
lrow = ws1.Cells(Rows.Count, 1).End(xlUp).Row + 1
'Reduce the copy/paste code down to what is needed.
ws2.Range("D12").Copy
ws1.Cells(lrow, 4).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'Merge and Left Align
Application.DisplayAlerts = False
With ws2.Range("D12:U12")
.Merge
.HorizontalAlignment = xlLeft
End With
Application.DisplayAlerts = True
End Sub
Display More
Let me know if you have any questions!
Sincerely,
Max
Re: Macro: Auto Generate Custom Serial Number
Hello!
I played around with this for a little bit and I think I have a solution for you. Place this code in the Worksheet Module Sheet1(Form):
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LastJO As Integer
Dim LastWA As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lrow As Long
'Serial Number
If Not Intersect(Target, Range("B2:B8")) Is Nothing Then
Set ws1 = ActiveSheet
Set ws2 = Sheets("Follow-up")
If Not Intersect(Target, Cells(2, 2)) Is Nothing Then
On Error GoTo Error1
LastJO = WorksheetFunction.Substitute(Names("LastJO"), "=", "")
GoTo Next1
If LastJO < 1 Then
Error1:
Names.Add Name:="LastJO", RefersTo:=1
LastJO = WorksheetFunction.Substitute(Names("LastJO"), "=", "")
End If
Resume Next
Next1:
On Error GoTo Error2
LastWA = WorksheetFunction.Substitute(Names("LastWA"), "=", "")
GoTo Next2
If LastWA < 1 Then
Error2:
Names.Add Name:="LastWA", RefersTo:=1
LastWA = WorksheetFunction.Substitute(Names("LastWA"), "=", "")
End If
Resume Next
Next2:
If Cells(2, 2) = "Normal" Then Cells(3, 2) = "JO-" & Year(Now) & Format(LastJO, "#00")
If Cells(2, 2) = "Warranty" Then Cells(3, 2) = "WA-" & Year(Now) & Format(LastWA, "#00")
If Cells(2, 2) = "Normal" Then Names("LastJO").Value = LastJO + 1
If Cells(2, 2) = "Warranty" Then Names("LastWA").Value = LastWA + 1
End If
'If Form is complete
If Range("B2") <> "" And Range("B3") <> "" And Range("B4") <> "" And Range("B5") <> "" And Range("B6") <> "" And Range("B7") <> "" And Range("B8") <> "" Then
lrow = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
ws2.Cells(lrow, 1) = "Request"
ws2.Cells(lrow, 4) = ws1.Cells(4, 2)
ws2.Cells(lrow, 5) = ws1.Cells(3, 2) 'Add your Vlookup here for cust name
ws2.Cells(lrow, 8) = ws1.Cells(3, 2)
ws2.Cells(lrow, 12) = Format(Now, "MM/DD/YYYY")
ws2.Cells(lrow, 13) = ws1.Cells(7, 2)
ws2.Cells(lrow, 14) = ws1.Cells(8, 2)
ws2.Cells(lrow, 15) = ws1.Cells(5, 2)
ws2.Cells(lrow, 16) = ws1.Cells(6, 2)
ws1.Cells(2, 2) = ""
ws1.Cells(3, 2) = ""
ws1.Cells(4, 2) = ""
ws1.Cells(5, 2) = ""
ws1.Cells(6, 2) = ""
ws1.Cells(7, 2) = ""
ws1.Cells(8, 2) = ""
End If
End If
End Sub
Display More
It's storing the last used serial number value for both JO and WA. This code will populate the follow-up sheet once the form is completely filled out. Then it will automatically clear the form.
Let me know how it works!
Sincerely,
Max
Re: Compile Particular Columns from Different Workbooks with Options
Awesome I'm glad I was able to figure it out!!! Have a nice holiday season!
Re: Linking Excel Files Read Only
This is how you can link the data as read only:
Data Tab-> From Other Sources -> From Microsoft Query -> Double click "Excel Files*" -> In the directory window (Right window) find the file path of your excel sheet (This is kind of a pain since the window is so small) -> Once the path is chosen, in the Database Name window (Left window), Select your excel workbook with the source data -> Check the "read only" box under the help button -> Click ok -> Select the sheet that contains your source data and click the arrow to move it into the window "Columns in your query:" -> Click Next 3 times -> Then Finish
Let me know if you have any problems here!
Sincerely,
Max
Re: Compile Particular Columns from Different Workbooks with Options
I think I fixed all the errors. I played around with it for a while and was able to add and remove items successfully. Also I am now having the remove/add combobox lists update everytime something is added or removed:
Option Explicit
Public wbFinal As Workbook
Public wb1 As Workbook
Public wb2 As Workbook
Public wb3 As Workbook
Public chk1 As String
Public chk2 As String
Public chk3 As String
Public wb1Name As String
Public wb2Name As String
Public wb3Name As String
Public FileExtension As String
Dim Deleteclms As Long
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim FilePath As String
Dim lrow1 As Long
Dim Lrow2 As Long
Dim Lclm As Long
Dim i As Integer
Dim Data As String
Dim wbname As String
'Variables for excel sheet names (Update these if necessary)
wb1Name = "Workbook_for_1st_Data-1"
wb2Name = "Workbook_for_2nd_Data-1"
wb3Name = "Workbook_for_3rd_Data"
FilePath = "Z:" 'File location of excel files
FileExtension = ".xlsx" 'File extension of excel files
Deleteclms = 100 'Sets variable to what columns need to be deleted that were used to hold values
'Defults workbook open check to No
chk1 = "N"
chk2 = "N"
chk3 = "N"
'If workbooks are open then change check to yes
For Each wb In Workbooks
If wb.Name = wb1Name & FileExtension Then chk1 = "Y"
If wb.Name = wb2Name & FileExtension Then chk2 = "Y"
If wb.Name = wb3Name & FileExtension Then chk3 = "Y"
Next wb
'Sets wb variables
Set wbFinal = ActiveWorkbook
'If workbooks are open then assign variable, if not then open workbooks and assign variable
If chk1 = "Y" Then Set wb1 = Workbooks(wb1Name & FileExtension) Else Set wb1 = Workbooks.Open(filename:=FilePath & "\" & wb1Name & FileExtension)
If chk2 = "Y" Then Set wb2 = Workbooks(wb2Name & FileExtension) Else Set wb2 = Workbooks.Open(filename:=FilePath & "\" & wb2Name & FileExtension)
If chk3 = "Y" Then Set wb3 = Workbooks(wb3Name & FileExtension) Else Set wb3 = Workbooks.Open(filename:=FilePath & "\" & wb3Name & FileExtension)
If Cells(1, 1) = "" Then GoTo Skip1
'Populates remove combo box:
wbFinal.Activate
Lclm = Cells(1, Columns.Count).End(xlToLeft).Column 'Finds last column
Range("A1", Cells(1, Lclm)).Copy 'Copies first column data over to side
Cells(2, Deleteclms + 5).PasteSpecial Transpose:=True 'Pastes the data in a list
lrow1 = Cells(Rows.Count, Deleteclms + 5).End(xlUp).Row 'Finds last row of list
If Cells(3, Deleteclms + 5) = "" Then
Me.ComboBox2.AddItem Cells(2, Deleteclms + 5)
Else
Me.ComboBox2.List = Range(Cells(2, Deleteclms + 5), Cells(lrow1, Deleteclms + 5)).Value 'Adds list to combo box
End If
Skip1:
'Find last row of column 100 (list column for combo box population)
lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, Deleteclms).End(xlUp).Row + 1
wb1.Activate
wb1.Sheets(1).Range(Cells(1, 1), wb1.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy 'Copies Column headers for workbook
wbFinal.Sheets(1).Cells(lrow1, Deleteclms).PasteSpecial Transpose:=True 'Transposes column headers for list
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, Deleteclms).End(xlUp).Row 'Finds last row of lsit
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(lrow1, Deleteclms + 1), Cells(Lrow2, Deleteclms + 1)) = wb1.Name 'Adds in proper workbookbname to list
lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, Deleteclms).End(xlUp).Row + 1
wb2.Activate
wb2.Sheets(1).Range(Cells(1, 1), wb2.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy
wbFinal.Sheets(1).Cells(lrow1, Deleteclms).PasteSpecial Transpose:=True
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, Deleteclms).End(xlUp).Row
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(lrow1, Deleteclms + 1), Cells(Lrow2, Deleteclms + 1)) = wb2.Name
lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, Deleteclms).End(xlUp).Row + 1
wb3.Activate
wb3.Sheets(1).Range(Cells(1, 1), wb3.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy
wbFinal.Sheets(1).Cells(lrow1, Deleteclms).PasteSpecial Transpose:=True
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, Deleteclms).End(xlUp).Row
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(lrow1, Deleteclms + 1), Cells(Lrow2, Deleteclms + 1)) = wb3.Name
'Populate add combo box
Me.ComboBox1.List = wbFinal.Sheets(1).Range(Cells(2, Deleteclms), Cells(Lrow2, Deleteclms)).Value 'Populated add combobox with list we just created (Workbook names will be used as a reference on which columns to add)
If Cells(1, 1) = "" Then GoTo Skip2
'Loops through each exisiting column to update them:
For i = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
Data = Cells(1, i)
wbname = Cells(WorksheetFunction.Match(Data, Columns(Deleteclms), 0), Deleteclms + 1)
Workbooks(wbname).Activate
Columns(WorksheetFunction.Match(Data, Rows(1), 0)).Copy
wbFinal.Activate
Cells(1, i).PasteSpecial Paste:=xlPasteValues
Next i
Skip2:
End Sub
Private Sub CommandButton1_Click()
Dim wbA As Workbook
Dim wbname As String
Dim Data As String
Dim clm1 As String
Dim lrow1 As Long
Set wbFinal = ActiveWorkbook
If wbFinal.Sheets(1).Cells(1, 1) = "" Then clm1 = "N" Else clm1 = "Y"
'If combobox 1 is populated then add the data
If Trim(Me.ComboBox1) <> "" Then
Data = Cells(WorksheetFunction.Match(Me.ComboBox1.Value, Columns(Deleteclms), 0), Deleteclms) 'Assigns data to variable
wbname = Cells(WorksheetFunction.Match(Me.ComboBox1.Value, Columns(Deleteclms), 0), Deleteclms + 1) 'Assigns workbook where the dat alives to variable
Workbooks(wbname).Activate
Columns(WorksheetFunction.Match(Data, Rows(1), 0)).Copy 'Copies desiered data
wbFinal.Activate
If clm1 = "Y" Then
wbFinal.Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).EntireColumn.PasteSpecial Paste:=xlValues 'Pastes data in workbook
Else
wbFinal.Sheets(1).Cells(1, Columns.Count).End(xlToLeft).EntireColumn.PasteSpecial Paste:=xlValues 'Pastes data in workbook
End If
ComboBox1.Value = "" 'Resets Combobox
lrow1 = Cells(Rows.Count, Deleteclms + 5).End(xlUp).Row
Cells(lrow1 + 1, Deleteclms + 5) = Data
ComboBox2.Clear
ComboBox2.List = Range(Cells(2, Deleteclms + 5), Cells(Rows.Count, Deleteclms + 5).End(xlUp).Offset(1, 0)).Value
MsgBox Data & " Column has been added." 'confirms action
End If
'If combobox 2 is populated then remove the data
If Trim(Me.ComboBox2) <> "" Then
Data = ComboBox2.Value
Columns(WorksheetFunction.Match(Data, Rows(1), 0)).Delete 'Deletes row with combobox2 text
Deleteclms = Deleteclms - 1 'Updates the columns hwere our lists data lives
ComboBox2.Value = "" 'Resets Combobox
Cells(WorksheetFunction.Match(Data, Columns(Deleteclms + 5), 0), Deleteclms + 5).Delete 'Deletes data from remove lists
lrow1 = Cells(Rows.Count, Deleteclms + 5).End(xlUp).Row 'Finds last row of list
Me.ComboBox2.Clear
If Cells(3, Deleteclms + 5) = "" Then
Me.ComboBox2.AddItem Cells(2, Deleteclms + 5)
Else
Me.ComboBox2.List = Range(Cells(2, Deleteclms + 5), Cells(lrow1, Deleteclms + 5)).Value 'Adds list to combo box
End If
MsgBox Data & " Column is removed." 'confirms action
End If
End Sub
Private Sub CommandButton2_Click()
'Sets variable to what columns need to be deleted that were used to hold values
If Deleteclms = 0 Then Deleteclms = 100
Range(Columns(Deleteclms), Columns(Deleteclms + 5)).ClearContents
'Closes workbooks if they were not previously opened
If chk1 = "N" Then Workbooks(wb1Name & FileExtension).Close False
If chk2 = "N" Then Workbooks(wb2Name & FileExtension).Close False
If chk3 = "N" Then Workbooks(wb3Name & FileExtension).Close False
Cells(1, 1).Select
Unload Me
End Sub
Private Sub UserForm_Terminate()
'Same as cancel button
If Deleteclms = 0 Then Deleteclms = 100
On Error Resume Next
Range(Columns(Deleteclms), Columns(Deleteclms + 5)).ClearContents
If chk1 = "N" Then Workbooks(wb1Name & FileExtension).Close False
If chk2 = "N" Then Workbooks(wb2Name & FileExtension).Close False
If chk3 = "N" Then Workbooks(wb3Name & FileExtension).Close False
Cells(1, 1).Select
End Sub
Display More
Let me know how this does!
Thanks!
Re: Compile Particular Columns from Different Workbooks with Options
Easy enough to fix! For your first problem:
"Everything is fine except that I can add columns, but can't remove them with having "out of range" error."
I have a solution for the error that is occurring, but first I wanted to clarify what Commandbutton2 does. Command button two should say "Close" or "Cancel". It is not meant for removing columns. Commandbutton1 will take care of both removing and adding columns. If the remove combo box is populated, then the remove code will kick in.
That being said, here is the new code:
Option Explicit
Public wbFinal As Workbook
Public wb1 As Workbook
Public wb2 As Workbook
Public wb3 As Workbook
Public chk1 As String
Public chk2 As String
Public chk3 As String
Public wb1Name As String
Public wb2Name As String
Public wb3Name As String
Public FileExtension As String
Dim Deleteclms As Long
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim FilePath As String
Dim Lrow1 As Long
Dim Lrow2 As Long
Dim Lclm As Long
Dim i As Integer
Dim Data As String
Dim wbname As String
'Variables for excel sheet names (Update these if necessary)
wb1Name = "Workbook_for_1st_Data-1"
wb2Name = "Workbook_for_2nd_Data-1"
wb3Name = "Workbook_for_3rd_Data"
FilePath = "Z:" 'File location of excel files
FileExtension = ".xlsx" 'File extension of excel files
'Defults workbook open check to No
chk1 = "N"
chk2 = "N"
chk3 = "N"
'If workbooks are open then change check to yes
For Each wb In Workbooks
If wb.Name = wb1Name & FileExtension Then chk1 = "Y"
If wb.Name = wb2Name & FileExtension Then chk2 = "Y"
If wb.Name = wb3Name & FileExtension Then chk3 = "Y"
Next wb
'Sets wb variables
Set wbFinal = ActiveWorkbook
'If workbooks are open then assign variable, if not then open workbooks and assign variable
If chk1 = "Y" Then Set wb1 = Workbooks(wb1Name & FileExtension) Else Set wb1 = Workbooks.Open(filename:=FilePath & "\" & wb1Name & FileExtension)
If chk2 = "Y" Then Set wb2 = Workbooks(wb2Name & FileExtension) Else Set wb2 = Workbooks.Open(filename:=FilePath & "\" & wb2Name & FileExtension)
If chk3 = "Y" Then Set wb3 = Workbooks(wb3Name & FileExtension) Else Set wb3 = Workbooks.Open(filename:=FilePath & "\" & wb3Name & FileExtension)
'Populates remove combo box:
wbFinal.Activate
Lclm = Cells(1, Columns.Count).End(xlToLeft).Column 'Finds last column
Range("A1", Cells(1, Lclm)).Copy 'Copies first column data over to side
Cells(2, 35).PasteSpecial Transpose:=True 'Pastes the data in a list
Lrow1 = Cells(Rows.Count, 35).End(xlUp).Row 'Finds last row of list
Me.ComboBox2.List = Range("AI2:AI" & Lrow1).Value 'Adds list to combo box
'Find last row of column 30 (list column for combo box population)
Lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row + 1
wb1.Activate
wb1.Sheets(1).Range(Cells(1, 1), wb1.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy 'Copies Column headers for workbook
wbFinal.Sheets(1).Cells(Lrow1, 30).PasteSpecial Transpose:=True 'Transposes column headers for list
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row 'Finds last row of lsit
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(Lrow1, 31), Cells(Lrow2, 31)) = wb1.Name 'Adds in proper workbookbname to list
Lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row + 1
wb2.Activate
wb2.Sheets(1).Range(Cells(1, 1), wb2.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy
wbFinal.Sheets(1).Cells(Lrow1, 30).PasteSpecial Transpose:=True
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(Lrow1, 31), Cells(Lrow2, 31)) = wb2.Name
Lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row + 1
wb3.Activate
wb3.Sheets(1).Range(Cells(1, 1), wb3.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy
wbFinal.Sheets(1).Cells(Lrow1, 30).PasteSpecial Transpose:=True
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(Lrow1, 31), Cells(Lrow2, 31)) = wb3.Name
'Populate add combo box
Me.ComboBox1.List = wbFinal.Sheets(1).Range("AD2:AD" & Lrow2).Value 'Populated add combobox with list we just created (Workbook names will be used as a reference on which columns to add)
'Loops through each exisiting column to update them:
For i = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
Data = Cells(1, i)
wbname = Cells(WorksheetFunction.Match(Data, Columns(30), 0), 31)
Workbooks(wbname).Activate
Columns(WorksheetFunction.Match(Data, Rows(1), 0)).Copy
wbFinal.Activate
Cells(1, i).PasteSpecial Paste:=xlPasteValues
Next i
End Sub
Private Sub CommandButton1_Click()
Dim wbA As Workbook
Dim wbname As String
Dim Data As String
'Sets variable to what columns need to be deleted that were used to hold values
Deleteclms = 30
Set wbFinal = ActiveWorkbook
'If combobox 1 is populated then add the data
If Trim(Me.ComboBox1) <> "" Then
Data = Cells(WorksheetFunction.Match(Me.ComboBox1.Value, Columns(30), 0), 30) 'Assigns data to variable
wbname = Cells(WorksheetFunction.Match(Me.ComboBox1.Value, Columns(30), 0), 31) 'Assigns workbook where the dat alives to variable
Workbooks(wbname).Activate
Columns(WorksheetFunction.Match(Data, Rows(1), 0)).Copy 'Copies desiered data
wbFinal.Activate
wbFinal.Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).EntireColumn.PasteSpecial Paste:=xlValues 'Pastes data in workbook
ComboBox1.Value = "" 'Resets Combobox
MsgBox ComboBox1 & " Column has been added." 'confirms action
End If
If Trim(Me.ComboBox2) <> "" Then
Columns(WorksheetFunction.Match(Me.ComboBox2, Rows(1), 0)).Delete 'Deletes row with combobox2 text
Deleteclms = Deleteclms - 1 'Updates the columns hwere our lists data lives
ComboBox1.Value = "" 'Resets Combobox
MsgBox ComboBox2 & " Column is removed." 'confirms action
End If
End Sub
Private Sub CommandButton2_Click()
'Sets variable to what columns need to be deleted that were used to hold values
If Deleteclms = 0 Then Deleteclms = 30
Range(Columns(Deleteclms), Columns(Deleteclms + 5)).ClearContents
'Closes workbooks if they were not previously opened
If chk1 = "N" Then Workbooks(wb1Name & FileExtension).Close False
If chk2 = "N" Then Workbooks(wb2Name & FileExtension).Close False
If chk3 = "N" Then Workbooks(wb3Name & FileExtension).Close False
Cells(1, 1).Select
Unload Me
End Sub
Private Sub UserForm_Terminate()
'Same as cancel button
If Deleteclms = 0 Then Deleteclms = 30
On Error Resume Next
Range(Columns(Deleteclms), Columns(Deleteclms + 5)).ClearContents
If chk1 = "N" Then Workbooks(wb1Name & FileExtension).Close False
If chk2 = "N" Then Workbooks(wb2Name & FileExtension).Close False
If chk3 = "N" Then Workbooks(wb3Name & FileExtension).Close False
Cells(1, 1).Select
End Sub
Display More
It now will update the existing columns with this line of code (*This update happens when the user form is called, not when the workbook is opened):
'Loops through each exisiting column to update them:
For i = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
Data = Cells(1, i)
wbname = Cells(WorksheetFunction.Match(Data, Columns(30), 0), 31)
Workbooks(wbname).Activate
Columns(WorksheetFunction.Match(Data, Rows(1), 0)).Copy
wbFinal.Activate
Cells(1, i).PasteSpecial Paste:=xlPasteValues
Next i
Let me know how it works!
Sincerely,
Max
Re: Indexing Top Values without Pivot
Oh I'm no where close to retirement! I'm only 27 haha, still have a long ways to go!
Re: Compile Particular Columns from Different Workbooks with Options
I just realized that what I made does not update the columns that are currently in existence (I'm not sure if these grow over time, I imagine they probably do). I may be able to add something to do this as well if you want. Let me know!
Re: Compile Particular Columns from Different Workbooks with Options
Hello again onexc,
It took me a while to put all this together, I hope it works for you!
This will require 4 controls on your userform:
CommandButton1 - Used to add or remove columns from your final workbook
CommandButton2 - Used to close/cancel the userform
Combobox1 - Used to indicate what columns you would like to add
Combobox2 - Used to indicate what columns you would like to remove
Once done, place this code in the userform module:
Option Explicit
Public wbFinal As Workbook
Public wb1 As Workbook
Public wb2 As Workbook
Public wb3 As Workbook
Public chk1 As String
Public chk2 As String
Public chk3 As String
Public wb1Name As String
Public wb2Name As String
Public wb3Name As String
Public FileExtension As String
Dim Deleteclms As Long
Private Sub UserForm_Initialize()
Dim wb As Workbook
Dim FilePath As String
Dim Lrow1 As Long
Dim Lrow2 As Long
Dim Lclm As Long
'Variables for excel sheet names (Update these if necessary)
wb1Name = "Workbook_for_1st_Data-1"
wb2Name = "Workbook_for_2nd_Data-1"
wb3Name = "Workbook_for_3rd_Data"
FilePath = "Z:" 'File location of excel files
FileExtension = ".xlsx" 'File extension of excel files
'Defults workbook open check to No
chk1 = "N"
chk2 = "N"
chk3 = "N"
'If workbooks are open then change check to yes
For Each wb In Workbooks
If wb.Name = wb1Name & FileExtension Then chk1 = "Y"
If wb.Name = wb2Name & FileExtension Then chk2 = "Y"
If wb.Name = wb3Name & FileExtension Then chk3 = "Y"
Next wb
'Sets wb variables
Set wbFinal = ActiveWorkbook
'If workbooks are open then assign variable, if not then open workbooks and assign variable
If chk1 = "Y" Then Set wb1 = Workbooks(wb1Name & FileExtension) Else Set wb1 = Workbooks.Open(filename:=FilePath & "\" & wb1Name & FileExtension)
If chk2 = "Y" Then Set wb2 = Workbooks(wb2Name & FileExtension) Else Set wb2 = Workbooks.Open(filename:=FilePath & "\" & wb2Name & FileExtension)
If chk3 = "Y" Then Set wb3 = Workbooks(wb3Name & FileExtension) Else Set wb3 = Workbooks.Open(filename:=FilePath & "\" & wb3Name & FileExtension)
'Populates remove combo box:
wbFinal.Activate
Lclm = Cells(1, Columns.Count).End(xlToLeft).Column 'Finds last column
Range("A1", Cells(1, Lclm)).Copy 'Copies first column data over to side
Cells(2, 35).PasteSpecial Transpose:=True 'Pastes the data in a list
Lrow1 = Cells(Rows.Count, 35).End(xlUp).Row 'Finds last row of list
Me.ComboBox2.List = Range("AI2:AI" & Lrow1).Value 'Adds list to combo box
'Find last row of column 30 (list column for combo box population)
Lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row + 1
wb1.Activate
wb1.Sheets(1).Range(Cells(1, 1), wb1.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy 'Copies Column headers for workbook
wbFinal.Sheets(1).Cells(Lrow1, 30).PasteSpecial Transpose:=True 'Transposes column headers for list
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row 'Finds last row of lsit
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(Lrow1, 31), Cells(Lrow2, 31)) = wb1.Name 'Adds in proper workbookbname to list
Lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row + 1
wb2.Activate
wb2.Sheets(1).Range(Cells(1, 1), wb2.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy
wbFinal.Sheets(1).Cells(Lrow1, 30).PasteSpecial Transpose:=True
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(Lrow1, 31), Cells(Lrow2, 31)) = wb2.Name
Lrow1 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row + 1
wb3.Activate
wb3.Sheets(1).Range(Cells(1, 1), wb3.Sheets(1).Cells(1, Columns.Count).End(xlToLeft)).Copy
wbFinal.Sheets(1).Cells(Lrow1, 30).PasteSpecial Transpose:=True
Lrow2 = wbFinal.Sheets(1).Cells(Rows.Count, 30).End(xlUp).Row
wbFinal.Activate
wbFinal.Sheets(1).Range(Cells(Lrow1, 31), Cells(Lrow2, 31)) = wb3.Name
'Populate add combo box
Me.ComboBox1.List = wbFinal.Sheets(1).Range("AD2:AD" & Lrow2).Value 'Populated add combobox with list we just created (Workbook names will be used as a reference on which columns to add)
End Sub
Private Sub CommandButton1_Click()
Dim wbA As Workbook
Dim wbName As String
Dim Data As String
'Sets variable to what columns need to be deleted that were used to hold values
Deleteclms = 30
Set wbFinal = ActiveWorkbook
'If combobox 1 is populated then add the data
If Trim(Me.ComboBox1) <> "" Then
Data = Cells(WorksheetFunction.Match(Me.ComboBox1.Value, Columns(30), 0), 30) 'Assigns data to variable
wbName = Cells(WorksheetFunction.Match(Me.ComboBox1.Value, Columns(30), 0), 31) 'Assigns workbook where the dat alives to variable
Workbooks(wbName).Activate
Columns(WorksheetFunction.Match(Data, Rows(1), 0)).Copy 'Copies desiered data
wbFinal.Activate
wbFinal.Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).EntireColumn.PasteSpecial Paste:=xlValues 'Pastes data in workbook
ComboBox1.Value = "" 'Resets Combobox
MsgBox ComboBox1 & " Column has been added." 'confirms action
End If
If Trim(Me.ComboBox2) <> "" Then
Columns(WorksheetFunction.Match(Me.ComboBox2, Rows(1), 0)).Delete 'Deletes row with combobox2 text
Deleteclms = Deleteclms - 1 'Updates the columns hwere our lists data lives
ComboBox1.Value = "" 'Resets Combobox
MsgBox ComboBox2 & " Column is removed." 'confirms action
End If
End Sub
Private Sub CommandButton2_Click()
'Sets variable to what columns need to be deleted that were used to hold values
If Deleteclms = 0 Then Deleteclms = 30
Range(Columns(Deleteclms), Columns(Deleteclms + 5)).ClearContents
'Closes workbooks if they were not previously opened
If chk1 = "N" Then Workbooks(wb1Name & FileExtension).Close False
If chk2 = "N" Then Workbooks(wb2Name & FileExtension).Close False
If chk3 = "N" Then Workbooks(wb3Name & FileExtension).Close False
Unload Me
End Sub
Private Sub UserForm_Terminate()
'Same as cancel button
If Deleteclms = 0 Then Deleteclms = 30
Range(Columns(Deleteclms), Columns(Deleteclms + 5)).ClearContents
If chk1 = "N" Then Workbooks(wb1Name & FileExtension).Close False
If chk2 = "N" Then Workbooks(wb2Name & FileExtension).Close False
If chk3 = "N" Then Workbooks(wb3Name & FileExtension).Close False
End Sub
Display More
This is quite a bit of code, so I annotated it the best I could that way you can understand whats going on.
Main things to note:
1. I am using columns 30-35 in your final workbook to hold some values. If you need these columns I can update the code to accomadate.
2. You will need to update the file path where the workbooks live, and the extensions of your workbooks (non-final) if it is not already .xlsx.
3. I named the sheets the same as what you attached, update if neccessary.
4. Make sure control names match what I have.
Here are the bits of code that may need your attention (Right at the top of the initialize sub:
'Variables for excel sheet names (Update these if necessary)
wb1Name = "Workbook_for_1st_Data-1"
wb2Name = "Workbook_for_2nd_Data-1"
wb3Name = "Workbook_for_3rd_Data"
FilePath = "Z:" 'File location of excel files
FileExtension = ".xlsx" 'File extension of excel files
I hope this works! Let me know what you think!
Sincerely,
Max