I do not really know how to explain this in a clear manner. Please see attached image
I have a table with 4 different columns, 2 are identical to each other (NAME and QTY). The goal is to compare the differences between the QTY, however, in order to do it. I must:
1. sort the data
2. match the data item by item
This is not a big deal with small table but with 10 thousand rows, it takes me a few days to do it.
Pleas help me, I appreciate.
My logic is:
1. Sorted the first two columns (NAME and QTY)
2. For each value of second two columns (NAME and QTY), check if it match with first two column. If true, the insert the value.
3. For values are not matched, insert to new rows with offset from the rows that are in first two columns but not in second two columns
Is this what you are trying?
Option Explicit
Sub Sample()
Dim ws As Worksheet
Dim lastRow As Long, i As Long, newRow As Long
Dim aCell As Range, SrchRange As Range
Set ws = Sheets("Sheet1")
With ws
.Columns("A:B").Copy .Columns("G:G")
.Columns("G:H").Sort Key1:=.Range("G2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
lastRow = .Range("G" & Rows.Count).End(xlUp).Row
newRow = lastRow
Set SrchRange = .Range("G2:G" & lastRow)
lastRow = .Range("C" & Rows.Count).End(xlUp).Row
.Range("I1").Value = "NAME": .Range("J1").Value = "QTY"
For i = 2 To lastRow
If Len(Trim(.Range("C" & i).Value)) <> 0 Then
Set aCell = SrchRange.Find(What:=.Range("C" & i).Value, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
.Range("I" & aCell.Row).Value = .Range("C" & i).Value
.Range("J" & aCell.Row).Value = .Range("D" & i).Value
Else
newRow = newRow + 1
.Range("I" & newRow).Value = .Range("C" & i).Value
.Range("J" & newRow).Value = .Range("D" & i).Value
End If
End If
Next
End With
End Sub
SNAPSHOT
Based on your above requirements, the logic totally changes and hence I am posting it as a different answer.
Also in your "This is Wonderful" snapshot above, there is a slight error. As per logic SAMPLE10 cannot come above SAMPLE11. It has to come after SAMPLE11.
See the below snapshot
And here is the code :)
Option Explicit
Sub sAMPLE()
Dim ws As Worksheet
Dim lastRow As Long, i As Long, newRow As Long, rw As Long
Dim aCell As Range, SrchRange As Range
Set ws = Sheets("Sheet1")
With ws
.Columns("A:B").Copy .Columns("G:G")
.Columns("G:H").Sort key1:=.Range("G2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
.Columns("H:H").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
lastRow = .Range("G" & Rows.Count).End(xlUp).Row
For i = 2 To lastRow
.Range("H" & i).Value = GetLastNumbers(.Range("G" & i).Value)
If .Range("H" & i).Value <> 0 Then
.Range("G" & i).Value = Left(.Range("G" & i).Value, _
Len(.Range("G" & i).Value) - Len(.Range("H" & i).Value))
End If
Next i
.Columns("G:H").Sort key1:=.Range("H2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
For i = 2 To lastRow
If .Range("H" & i).Value <> 0 Then _
.Range("G" & i).Value = .Range("G" & i).Value & .Range("H" & i).Value
Next i
.Columns("H:H").Delete
newRow = lastRow
Set SrchRange = .Range("G2:G" & lastRow)
lastRow = .Range("C" & Rows.Count).End(xlUp).Row
.Range("I1").Value = "NAME": .Range("J1").Value = "QTY"
For i = 2 To lastRow
If Len(Trim(.Range("C" & i).Value)) <> 0 Then
Set aCell = SrchRange.Find(What:=.Range("C" & i).Value, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
.Range("I" & aCell.Row).Value = .Range("C" & i).Value
.Range("J" & aCell.Row).Value = Application.Evaluate("=SUMPRODUCT((C2:C" & lastRow _
& "=" & """" & .Range("C" & i).Value & """" & ")*(D2:D" & lastRow & "))")
Else
newRow = newRow + 1
.Range("I" & newRow).Value = .Range("C" & i).Value
.Range("J" & newRow).Value = .Range("D" & i).Value
End If
End If
Next
lastRow = .Range("G" & Rows.Count).End(xlUp).Row
For i = lastRow To 2 Step -1
If .Range("G" & i).Value = .Range("G" & i - 1).Value Then
.Range("H" & i - 1).Value = .Range("H" & i).Value + .Range("H" & i - 1).Value
If Application.WorksheetFunction.CountA(.Range("I" & i & ":J" & i)) = 0 Then
.Range("G" & i & ":J" & i).Delete Shift:=xlUp
Else
.Range("G" & i & ":H" & i).Delete Shift:=xlUp
End If
End If
Next i
lastRow = .Range("I" & Rows.Count).End(xlUp).Row
newRow = .Range("G" & Rows.Count).End(xlUp).Row
If lastRow <= newRow Then Exit Sub
.Range("I" & newRow & ":J" & lastRow).Sort key1:=.Range("I" & newRow), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
For i = lastRow To newRow Step -1
If .Range("I" & i).Value = .Range("I" & i - 1).Value Then
.Range("J" & i - 1).Value = .Range("J" & i).Value + .Range("J" & i - 1).Value
.Range("I" & i & ":J" & i).Delete Shift:=xlUp
End If
Next i
End With
End Sub
Function GetLastNumbers(strVal As String) As Long
Dim j As Long, strTemp As String
For j = Len(strVal) To 1 Step -1
If Not IsNumeric(Mid(strVal, j, 1)) Then Exit For
strTemp = Mid(strVal, j, 1) & strTemp
Next j
GetLastNumbers = Val(Trim(strTemp))
End Function
Related
The below code works as long as there are more than one instance of the search criteria. However, if there is only one row that is listed as the what in the find function I receive the error "Could not set the list property. Invalid property array index"
Private Sub UserForm_Initialize()
Dim iRow As Integer, iMax As Integer
iRow = Cells.Find(What:="New Jersey Audit Adjustment", _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
iMax = Cells.Find(What:="New Jersey Audit Adjustment", _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
MatchCase:=False, SearchFormat:=False).Row
Me.ComboBox1.List = Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox2.List = Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox3.List = Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox4.List = Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Value
Me.ComboBox5.List = Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Value
End Sub
The error occurs here Me.ComboBox1.List = Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Value if I have one row listed with "New Jersey Audit Adjustment"
When your range contains one cell, the .value will give you a value instead of an array. As the .list expects an array you could fill an array with one element or use addItem (see below)
If Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Cells.Count = 1 Then
Me.ComboBox1.AddItem Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Value
Else
Me.ComboBox1.List = Worksheets("C-Proposal-19").Range("B" & iRow & ":B" & iMax).Value
End If
I have a task where I need to get the last 125 data from an excel workbook copied to another workbook. And I want the user to select from a file browser the excel file where the data has been stored. The data will always in the the range of C17:C2051, F17:F2051 and goes on...
At last I want to put two formula above these ranges.
There are the formulas:
=AVARAGE(INDEX(C17:C2051;MATCH(MAX(C17:C2051);C17:C2051;1)):INDEX(C17:C2051;MAX(1;MATCH(MAX(C17:C2051);C17:C2051;1)-124)))
=STDEV(INDEX(C17:C2051;MATCH(MAX(C17:C2051);C17:C2051;1)):INDEX(C17:C2051;MAX(1;MATCH(MAX(C17:C2051);C17:C2051;1)-124)))
I wrote some code but right now it's actually doing nothing.
Sub Get_Data_From_File()
Dim FileToOpen As String
Dim File As Workbook
FileToOpen = Application.GetOpenFilename("Excel files (*.xlsx), *.xlsx")
Dim LastRow As Long
Dim Last8Rows As Range
LastRow = File.Range("D" & File.Rows.Count).End(xlUp).Row
Set Last8Rows = File.Range("C" & LastRow - 7)
Last8Rows.Copy
End Sub
This should get you started:
Sub Get_Data_From_File()
Const START_ROW As Long = 17
Const NUM_ROWS As Long = 125
Dim FileToOpen As String
Dim wb As Workbook, ws As Worksheet, wsDest As Worksheet
Dim LastRow As Long, FirstRow As Long
Dim LastRows As Range
FileToOpen = Application.GetOpenFilename("Excel files (*.xlsx), *.xlsx", _
Title:="Select file to import from")
If FileToOpen = False Then Exit Sub 'no file selected
Set wsDest = ActiveSheet 'pasting here; or specfy some other sheet...
Set wb = Workbooks.Open(FileToOpen, ReadOnly:=True)
Set ws = wb.Worksheets("data") 'or whatever sheet you need
LastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row 'find last row
If LastRow < START_ROW Then LastRow = START_ROW
FirstRow = IIf(LastRow - NUM_ROWS >= START_ROW, LastRow - NUM_ROWS, START_ROW) 'find first row
'copy ranges
ws.Range("C" & FirstRow & ":C" & LastRow).Copy wsDest.Cells(START_ROW, "C")
ws.Range("F" & FirstRow & ":F" & LastRow).Copy wsDest.Cells(START_ROW, "F")
'Add the formulas (note you need the US-format when using .Formula
' or you can use your local format with .FormulaLocal
wb.Close False 'no save
End Sub
After all these days it's working finally. I modified some lines in the code and it's doing the job. Thanks again #TimWilliams!
Here's my solution:
Sub Get_Data_From_File()
Const START_ROW As Long = 17
Const NUM_ROWS As Long = 124
Dim FileToOpen As String
Dim wb As Workbook, ws As Worksheet, wsDest As Worksheet
Dim LastRow As Long, FirstRow As Long
Dim LastRows As Range
FileToOpen = Application.GetOpenFilename("Excel files (*.xlsx), *.xlsx", _
Title:="Select file to import from") 'no file selected
Set wsDest = ActiveSheet 'pasting here; or specfy some other sheet...
Set wb = Workbooks.Open(FileToOpen, ReadOnly:=True)
Set ws = wb.Worksheets("SMI_650_Lxy") 'or whatever sheet you need
LastRow = ws.Cells(ws.Rows.Count, "F").End(xlUp).Row 'find last row
If LastRow < START_ROW Then LastRow = START_ROW
FirstRow = IIf(LastRow - NUM_ROWS >= START_ROW, LastRow - NUM_ROWS, START_ROW) 'find first row
Debug.Print "FirstRow" & vbTab & FirstRow 'test
Debug.Print "LastRow" & vbTab & LastRow
Debug.Print "START_ROW" & vbTab & START_ROW
'copy ranges
ws.Range("C" & FirstRow & ":C" & LastRow).Copy wsDest.Cells(START_ROW, "C")
ws.Range("F" & FirstRow & ":F" & LastRow).Copy wsDest.Cells(START_ROW, "F")
ws.Range("M" & FirstRow & ":M" & LastRow).Copy wsDest.Cells(START_ROW, "M") 'formula
ws.Range("P" & FirstRow & ":P" & LastRow).Copy wsDest.Cells(START_ROW, "P")
ws.Range("S" & FirstRow & ":S" & LastRow).Copy wsDest.Cells(START_ROW, "S")
ws.Range("V" & FirstRow & ":V" & LastRow).Copy wsDest.Cells(START_ROW, "V")
ws.Range("Y" & FirstRow & ":Y" & LastRow).Copy wsDest.Cells(START_ROW, "Y")
ws.Range("AF" & FirstRow & ":AF" & LastRow).Copy wsDest.Cells(START_ROW, "AF") 'formula
ws.Range("AM" & FirstRow & ":AM" & LastRow).Copy wsDest.Cells(START_ROW, "AM") 'formula
ws.Range("AP" & FirstRow & ":AP" & LastRow).Copy wsDest.Cells(START_ROW, "AP")
ws.Range("AS" & FirstRow & ":AS" & LastRow).Copy wsDest.Cells(START_ROW, "AS")
ws.Range("AV" & FirstRow & ":AV" & LastRow).Copy wsDest.Cells(START_ROW, "AV")
ws.Range("AY" & FirstRow & ":AY" & LastRow).Copy wsDest.Cells(START_ROW, "AY")
ws.Range("BB" & FirstRow & ":BB" & LastRow).Copy wsDest.Cells(START_ROW, "BB")
ws.Range("BE" & FirstRow & ":BE" & LastRow).Copy wsDest.Cells(START_ROW, "BE")
ws.Range("BL" & FirstRow & ":BL" & LastRow).Copy wsDest.Cells(START_ROW, "BL") 'formula
ws.Range("BS" & FirstRow & ":BS" & LastRow).Copy wsDest.Cells(START_ROW, "BS") 'formula
ws.Range("BV" & FirstRow & ":BV" & LastRow).Copy wsDest.Cells(START_ROW, "BV")
ws.Range("BZ" & FirstRow & ":BZ" & LastRow).Copy wsDest.Cells(START_ROW, "BZ")
ws.Range("CD" & FirstRow & ":CD" & LastRow).Copy wsDest.Cells(START_ROW, "CD")
ws.Range("CH" & FirstRow & ":CH" & LastRow).Copy wsDest.Cells(START_ROW, "CH")
ws.Range("CK" & FirstRow & ":CK" & LastRow).Copy wsDest.Cells(START_ROW, "CK")
ws.Range("CN" & FirstRow & ":CN" & LastRow).Copy wsDest.Cells(START_ROW, "CN")
ws.Range("CQ" & FirstRow & ":CQ" & LastRow).Copy wsDest.Cells(START_ROW, "CQ")
ws.Range("CT" & FirstRow & ":CT" & LastRow).Copy wsDest.Cells(START_ROW, "CT")
ws.Range("CW" & FirstRow & ":CW" & LastRow).Copy wsDest.Cells(START_ROW, "CW")
ws.Range("CZ" & FirstRow & ":CZ" & LastRow).Copy wsDest.Cells(START_ROW, "CZ")
ws.Range("DC" & FirstRow & ":DC" & LastRow).Copy wsDest.Cells(START_ROW, "DC")
ws.Range("DF" & FirstRow & ":DF" & LastRow).Copy wsDest.Cells(START_ROW, "DF")
'Add the formulas (note you need the US-format when using .Formula
' or you can use your local format with .FormulaLocal
wb.Close False 'no save
End Sub
I've created some code to let users fill in a excelsheet with 4 tables. by pressing a button it copies the table data to another workbook. it has to find the last row filled and copy the data below that. Some cells won't get filled so i used .Filldown to get the empty cells filled. In order to make the .Filldown work i had to count the rows in the tables.
It all works but I'm new in VBA so i was wondering if my code can be simpelfied. It looks like a lot of code.
Private Sub CommandButton5_Click()
Dim PassWord As Variant
PassWord = InputBox("Wachtwoord?")
'PassWord = "Something"
If PassWord = "Something" Then
Dim nT1 As Integer
Dim nT2 As Integer
Dim nT3 As Integer
Dim nT4 As Integer
If Sheets("Variabelen").Range("H2") = 0 Then
Set Z = ActiveWorkbook.Sheets(1)
Set T1 = ActiveSheet.ListObjects("Tabel1").DataBodyRange
Set T1C = ActiveSheet.ListObjects("Tabel1")
Set T2 = ActiveSheet.ListObjects("Tabel2").DataBodyRange
Set T2C = ActiveSheet.ListObjects("Tabel2")
Set T3 = ActiveSheet.ListObjects("Tabel3").DataBodyRange
Set T3C = ActiveSheet.ListObjects("Tabel3")
Set T4 = ActiveSheet.ListObjects("Tabel4").DataBodyRange
Set T4C = ActiveSheet.ListObjects("Tabel4")
nT1 = T1C.Range.Rows.Count - 1
nT2 = T2C.Range.Rows.Count - 1
nT3 = T3C.Range.Rows.Count - 1
nT4 = T4C.Range.Rows.Count - 1
'Test_ verwijderen als bestand actief wordt
Set Y = Workbooks.Open("\\Somewhere\Test_Masterbestand Afdeling.xlsx")
'Huidige medewerker in opleiding (T1)
lRow = Y.Worksheets("Data").Cells(Y.Worksheets("Data").Rows.Count, 1).End(xlUp).Row
lRow = lRow + 1
T1.Copy
Y.Worksheets("Data").Range("A" & lRow).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Y.Sheets("Data").Range("I" & lRow).Value = "Huidige medewerker in opleiding"
Y.Sheets("Data").Range("J" & lRow).Value = Z.Range("C3").Value
Y.Sheets("Data").Range("K" & lRow).Value = Z.Range("C4").Value
Y.Sheets("Data").Range("L" & lRow).Value = Z.Range("C6").Value
Dim LastRowA As Long
Dim LastRowB As Long
Dim LastRowC As Long
Dim LastRowD As Long
Dim LastRowE As Long
LastRowA = ActiveSheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowB = ActiveSheet.Range("I:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowC = ActiveSheet.Range("J:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowD = ActiveSheet.Range("K:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowE = ActiveSheet.Range("L:L").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
CopyrangeB = "I" & LastRowB & ":" & "I" & LastRowA
CopyrangeC = "J" & LastRowC & ":" & "J" & LastRowA
CopyrangeD = "K" & LastRowD & ":" & "K" & LastRowA
CopyrangeE = "L" & LastRowE & ":" & "L" & LastRowA
If nT1 > 1 Then
ActiveSheet.Range(CopyrangeB).FillDown
ActiveSheet.Range(CopyrangeC).FillDown
ActiveSheet.Range(CopyrangeD).FillDown
ActiveSheet.Range(CopyrangeE).FillDown
End If
'Nieuwe instroom in opleiding (T2)
lRow = Y.Worksheets("Data").Cells(Y.Worksheets("Data").Rows.Count, 1).End(xlUp).Row
lRow = lRow + 1
T2.Copy
Y.Worksheets("Data").Range("A" & lRow).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Y.Sheets("Data").Range("I" & lRow).Value = "Nieuwe instroom in opleiding"
Y.Sheets("Data").Range("J" & lRow).Value = Z.Range("C3").Value
Y.Sheets("Data").Range("K" & lRow).Value = Z.Range("C4").Value
Y.Sheets("Data").Range("L" & lRow).Value = Z.Range("C6").Value
Dim LastRowF As Long
Dim LastRowG As Long
Dim LastRowH As Long
Dim LastRowI As Long
Dim LastRowJ As Long
LastRowF = ActiveSheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowG = ActiveSheet.Range("I:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowH = ActiveSheet.Range("J:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowI = ActiveSheet.Range("K:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowJ = ActiveSheet.Range("L:L").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
CopyrangeG = "I" & LastRowG & ":" & "I" & LastRowF
CopyrangeH = "J" & LastRowH & ":" & "J" & LastRowF
CopyrangeI = "K" & LastRowI & ":" & "K" & LastRowF
CopyrangeJ = "L" & LastRowJ & ":" & "L" & LastRowF
If nT2 > 1 Then
ActiveSheet.Range(CopyrangeG).FillDown
ActiveSheet.Range(CopyrangeH).FillDown
ActiveSheet.Range(CopyrangeI).FillDown
ActiveSheet.Range(CopyrangeJ).FillDown
End If
'Afdelingspecifiek(T3)
lRow = Y.Worksheets("Data").Cells(Y.Worksheets("Data").Rows.Count, 1).End(xlUp).Row
lRow = lRow + 1
T3.Copy
Y.Worksheets("Data").Range("A" & lRow).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Y.Sheets("Data").Range("I" & lRow).Value = "Afdelingspecifiek"
Y.Sheets("Data").Range("J" & lRow).Value = Z.Range("C3").Value
Y.Sheets("Data").Range("K" & lRow).Value = Z.Range("C4").Value
Y.Sheets("Data").Range("L" & lRow).Value = Z.Range("C6").Value
Dim LastRowK As Long
Dim LastRowL As Long
Dim LastRowM As Long
Dim LastRowN As Long
Dim LastRowO As Long
LastRowK = ActiveSheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowL = ActiveSheet.Range("I:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowM = ActiveSheet.Range("J:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowN = ActiveSheet.Range("K:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowO = ActiveSheet.Range("L:L").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
CopyrangeL = "I" & LastRowL & ":" & "I" & LastRowK
CopyrangeM = "J" & LastRowM & ":" & "J" & LastRowK
CopyrangeN = "K" & LastRowN & ":" & "K" & LastRowK
CopyrangeO = "L" & LastRowO & ":" & "L" & LastRowK
If nT3 > 1 Then
ActiveSheet.Range(CopyrangeL).FillDown
ActiveSheet.Range(CopyrangeM).FillDown
ActiveSheet.Range(CopyrangeN).FillDown
ActiveSheet.Range(CopyrangeO).FillDown
End If
'Individueel (T4)
lRow = Y.Worksheets("Data").Cells(Y.Worksheets("Data").Rows.Count, 1).End(xlUp).Row
lRow = lRow + 1
T4.Copy
Y.Worksheets("Data").Range("A" & lRow).PasteSpecial Paste:=xlPasteValues
Y.Sheets("Data").Range("I" & lRow).Value = "Individueel"
Y.Sheets("Data").Range("J" & lRow).Value = Z.Range("C3").Value
Y.Sheets("Data").Range("K" & lRow).Value = Z.Range("C4").Value
Y.Sheets("Data").Range("L" & lRow).Value = Z.Range("C6").Value
Application.CutCopyMode = False
Dim LastRowP As Long
Dim LastRowQ As Long
Dim LastRowR As Long
Dim LastRowS As Long
Dim LastRowT As Long
LastRowP = ActiveSheet.Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowQ = ActiveSheet.Range("I:I").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowR = ActiveSheet.Range("J:J").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowS = ActiveSheet.Range("K:K").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowT = ActiveSheet.Range("L:L").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastRowU = ActiveSheet.Range("N:N").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
CopyrangeQ = "I" & LastRowQ & ":" & "I" & LastRowP
CopyrangeR = "J" & LastRowR & ":" & "J" & LastRowP
CopyrangeS = "K" & LastRowS & ":" & "K" & LastRowP
CopyrangeT = "L" & LastRowT & ":" & "L" & LastRowP
'Formule in Kolom M
CopyrangeU = "N" & LastRowU & ":" & "N" & LastRowP
If nT4 > 1 Then
ActiveSheet.Range(CopyrangeQ).FillDown
ActiveSheet.Range(CopyrangeR).FillDown
ActiveSheet.Range(CopyrangeS).FillDown
ActiveSheet.Range(CopyrangeT).FillDown
ActiveSheet.Range(CopyrangeU).FillDown
End If
' Y.Close (True)
'Quote weghalen bij opleveren
'Sheets("Variabelen").Range("H2").Value = Sheets("Variabelen").Range("H2").Value + 1
Else
MsgBox ("Niet nog een keer Sylvia!!!!")
End If
Else
'Do nothing
End If
End Sub
None
I have one worksheet called mainData, which contains all data for ten products.
When I enter new data in mainData, I want to automatically copy the new data into the last row of another product worksheet. When I enter new data into mainData, how can I recognize the new data belongs to which product's worksheet, hence copy the new data into the product worksheet?
I'm stuck in copying it to another worksheet because I need to copy it to another ten worksheets according to product's type.
Here's what I've done to the mainData:
With Sheets("mainData")
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
.Range("B" & LastRow) = ComboBox1.Text
.Range("C" & LastRow) = TextBox1.Text
.Range("D" & LastRow) = TextBox2.Text
.Range("E" & LastRow) = TextBox3.Text
.Range("F" & LastRow) = TextBox4.Text
.Range("G" & LastRow) = TextBox5.Text
.Range("H" & LastRow) = ComboBox2.Text
.Range("I" & LastRow) = TextBox6.Text
.Range("J" & LastRow) = TextBox7.Text
.Range("K" & LastRow) = TextBox8.Text
Range("B32:B320").Select
ActiveWorkbook.Worksheets("mainData").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("mainData").Sort.SortFields.Add Key:=Range("B32:B305") _
, SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
"prod1, prod2, prod3, prod4, prod5, prod6, prod7, prod8, prod9, prod10" _
, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("mainData").Sort
.SetRange Range("B32:W305")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
here's what i mean.when i enter new prod1 data into mainData worksheet, i want to automatically copy it into the last row of product 1 worksheet. i may enter many type of product i.e prod2,prod4 into mainData so how to copy this data into its particular product worksheet?
Is this what you are trying? (UNTESTED)
Also I have not done any error handling. I am sure you will take care of it :)
Dim prd As String
Dim ws As Worksheet
Dim LastRow As Long
'~~> Extract the number from the combobox
prd = Trim(Replace(ComboBox1.Text, "prod", ""))
'~~> Decide which sheet the data needs to be written to
'~~> Please ensure that sheets have names like "Product 1", "Product 2" etc
Set ws = ThisWorkbook.Sheets("Product " & prd)
'~~> Update it to the relevant sheet
With ws
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
.Range("B" & LastRow) = ComboBox1.Value
.Range("C" & LastRow) = TextBox1.Text
.Range("D" & LastRow) = TextBox2.Text
.Range("E" & LastRow) = TextBox3.Text
.Range("F" & LastRow) = TextBox4.Text
.Range("G" & LastRow) = TextBox5.Text
.Range("H" & LastRow) = ComboBox2.Value
.Range("I" & LastRow) = TextBox6.Text
.Range("J" & LastRow) = TextBox7.Text
.Range("K" & LastRow) = TextBox8.Text
'~~> Sort the data
With .Range("B2:W" & LastRow)
.Sort Key1:=ws.Range("B2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
End With
'~~> Update it in mainData
With Sheets("mainData")
LastRow = .Range("B" & .Rows.Count).End(xlUp).Row + 1
.Range("B" & LastRow) = ComboBox1.Value
.Range("C" & LastRow) = TextBox1.Text
.Range("D" & LastRow) = TextBox2.Text
.Range("E" & LastRow) = TextBox3.Text
.Range("F" & LastRow) = TextBox4.Text
.Range("G" & LastRow) = TextBox5.Text
.Range("H" & LastRow) = ComboBox2.Value
.Range("I" & LastRow) = TextBox6.Text
.Range("J" & LastRow) = TextBox7.Text
.Range("K" & LastRow) = TextBox8.Text
'~~> Sort the data
With .Range("B2:W" & LastRow)
.Sort Key1:=Sheets("mainData").Range("B2"), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End With
End With
I have the following code block to take out various errors and assign an error code description to the data. It works fine as long as the filter returns a result. If it does not then it deletes the header row. How can I prevent that from happening? Thanks in advance.
Sheets("Tempsheet").Select
Range("A1:K1").AutoFilter
Range("A1:K1").AutoFilter Field:=5, Criteria1:="0", Criteria2:=0
Range("K2:K" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "Excluded: $0.00 Amount"
Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Copy
Sheets("Excluded").Select
Range("A2").PasteSpecial
Sheets("Tempsheet").Select
Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Delete
Sheets("Tempsheet").AutoFilterMode = False
If no data is returned by the filter then Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row) will return row 1, so test for row > 1 before doing the Delete
If Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).Row > 1 then
... .Delete
End If
Something like this code which tests for a filter result should do it
Dim ws As Worksheet
Dim ws2 As Worksheet
Set ws = Sheets("Tempsheet")
Set ws2 = Sheets("Excluded")
Set rng1 = ws.Range(ws.[a1], ws.Cells(Rows.Count, "k").End(xlUp))
rng1.AutoFilter Field:=5, Criteria1:="0", Criteria2:=0
If rng1.SpecialCells(xlVisible).Rows.Count > 1 Then
ws.Range("K2:K" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "Excluded: $0.00 Amount"
ws.Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Copy
ws2.[a2].PasteSpecial Paste:=xlPasteValues
rng1.Offset(1, 0).Resize(rng1.SpecialCells(xlVisible).Rows.Count - 1).EntireRow.Delete
End If
Sheets("Tempsheet").AutoFilterMode = False
Sheets("Tempsheet").Select
Range("A1:K1").AutoFilter
Range("A1:K1").AutoFilter Field:=5, Criteria1:="0", Criteria2:=0
Range("K2:K" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "Excluded: $0.00 Amount"
Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Copy
Sheets("Excluded").Select
Range("A2").PasteSpecial
Sheets("Tempsheet").Select
if Range("A" & Rows.Count).End(xlUp).Row > 1 then
Range("A2:K" & Range("A" & Rows.Count).End(xlUp).Row).EntireRow.Delete
end if
Sheets("Tempsheet").AutoFilterMode = False