Excel VBA bug query - excel

I currently am using a userform I created to log data into a spreadsheet. During data validation, I notice that when I key in dates in the DD/MM/YYYY format in the userform, some rows swap the DD/MM to MM/DD, which causes confusion downstream.
I adjusted the data type for the entire column, but the userform code seems act differently for the particular row. Is this a bug or am I overlooking a line of code somewhere?
Here are the images of the userform and the data in the spreadsheet, as well as the code for the information transfer segment.
Data from spreadsheet
Userform Date segment
Code for transfer information:
'Transfer information
Cells(emptyRow, 1).Value = p
Cells(emptyRow, 3).Value = hour.Value & ":" & minute.Value & " " & ampm.Value
Cells(emptyRow, 4).Value = PTID.Value
Cells(emptyRow, 2).Value = cmbdate.Value & "/" & cmbmonth.Value & "/" & cmbyear.Value
Cells(emptyRow, 5).Value = UNIT.Value
Cells(emptyRow, 6).Value = PCBOX.Value
Cells(emptyRow, 7).Value = WASTE.Value
Cells(emptyRow, 8).Value = REPORTED.Value
Cells(emptyRow, 9).Value = DETBOX.Value
Cells(emptyRow, 10).Value = FOLBOX.Value
Cells(emptyRow, 11).Value = SUMBOX.Value
Cells(emptyRow, 12).Value = CAPBOX.Value
Cells(emptyRow, 13).Value = EHOR.Value
Cells(emptyRow, 14).Value = TECHS.Value & "," & TECHS2.Value & "," & TECHS3.Value & "," & TECHS4.Value
Cells(emptyRow, 15).Value = ERRORBOX.Value
Cells(emptyRow, 16).Value = PREVBOX.Value
Cells(emptyRow, 17).Value = SOP.Value
Cells(emptyRow, 18).Value = AUDIFILE.Value
Cells(emptyRow, 19).Value = INTERFILE.Value
Cells(emptyRow, 20).Value = cmbdate2.Value & "/" & cmbmonth2.Value & "/" & cmbyear2.Value
Cells(emptyRow, 23).Value = Phase.Value
Cells(emptyRow, 24).Value = QIM.Value
MsgBox "Please check your entry in the sheet", , "Entry Complete"
MsgBox "Your entry serial number is " & p
SN.Text = p
VNCFORM.Hide
Code for recall information (to same userform, when a serial number for the entry is entered into the userform)
Private Sub SN_AfterUpdate()
'TO RETRIEVE S/N DATA TO THE USERFORM'
Dim x As Range
Dim y As Long
Set WS = Worksheets("Data")
y = Application.WorksheetFunction.Match(CLng(Me.SN.Value), WS.Range("A:A"), 0)
'POSSIBLE PROBLEM AREA'
Me.cmbdate.Value = Left(WS.Range("B" & y).Value, 2)
Me.cmbmonth.Value = Mid(WS.Range("B" & y).Value, 4, 2)
Me.cmbyear.Value = Right(WS.Range("B" & y).Value, 4)
Me.hour.Value = CStr(Left(WS.Range("C" & y).Value, 2))
Me.minute.Value = CStr(Mid(WS.Range("C" & y).Value, 4, 2))
Me.ampm.Value = CStr(Right(WS.Range("C" & y).Value, 2))
Me.PTID.Value = WS.Range("D" & y).Value
Me.UNIT.Value = WS.Range("E" & y).Value
Me.PCBOX.Value = WS.Range("F" & y).Value
Me.WASTE.Value = WS.Range("G" & y).Value
Me.REPORTED.Value = WS.Range("H" & y).Value
Me.DETBOX.Value = WS.Range("I" & y).Value
Me.FOLBOX.Value = WS.Range("J" & y).Value
Me.SUMBOX.Value = WS.Range("K" & y).Value
Me.CAPBOX.Value = WS.Range("L" & y).Value
Me.EHOR.Value = WS.Range("M" & y).Value
'Techs involved in case transcribed back to userform
Dim MYARRAY() As String, MYSTRING As String
MYSTRING = WS.Range("N" & y).Value
MYARRAY = Split(MYSTRING, ",")
For N = 0 To UBound(MYARRAY)
Me.TECHS.Value = MYARRAY(0)
Me.TECHS2.Value = MYARRAY(1)
Me.TECHS3.Value = MYARRAY(2)
Me.TECHS4.Value = MYARRAY(3)
Next N
Me.ERRORBOX.Value = WS.Range("O" & y).Value
Me.PREVBOX.Value = WS.Range("P" & y).Value
Me.SOP.Value = WS.Range("Q" & y).Value
Me.AUDIFILE.Value = WS.Range("R" & y).Value
Me.INTERFILE.Value = WS.Range("S" & y).Value
Me.cmbdate2.Value = Left(WS.Range("T" & y).Value, 2)
Me.cmbmonth2.Value = Mid(WS.Range("T" & y).Value, 4, 2)
Me.cmbyear2.Value = Right(WS.Range("T" & y).Value, 4)
Me.Phase.Value = WS.Range("W" & y).Value
Me.QIM.Value = WS.Range("X" & y).Value
End Sub
The problem seems to occur when I recall data back into the userform where the month value and the date values get swapped for some reason.
Is there a property of code I am overlooking? Or could I improve the code somehow; I think the error comes from the recall segment (see: 'POSSIBLE PROBLEM AREA')

I would make sure when writing/reading dates you're being more explicit:
'write date to sheet
Cells(emptyRow, 2).Value = DateSerial(CLng(cmbyear.Value), _
CLng(cmbmonth.Value), _
CLng(cmbdate.Value))
'read date from sheet
Dim dt As Date
dt = WS.Range("B" & y).Value
Me.cmbdate.Value = Day(dt)
Me.cmbmonth.Value = Month(dt)
Me.cmbyear.Value = Year(dt)
Also likely need some code to check there's an entry before trying to write/read it.

Related

Why is data being added to other worksheets in this particular code?

Using a userform, I have created an 'Update' function to the form to make amendments to certain rows of data.
For some reason, when I make an amendment and click on the 'Update' button, the data from columns T, P and W are randomly added to Sheet1 instead of Sheet2.
Private Sub Update_Click()
Dim selectedRow As Long
'Make Sheet2 active
Sheet2.Activate
Dim x As Range
Set WS = Worksheets("Data")
selectedRow = Application.WorksheetFunction.Match(CLng(Me.SN.Value), WS.Range("A:A"), 0)
'Transfer information
Cells(selectedRow, 3).Value = hour.Value & ":" & minute.Value & " " & ampm.Value
Cells(selectedRow, 4).Value = PTID.Value
Cells(selectedRow, 2).Value = cmbdate.Value & "/" & cmbmonth.Value & "/" & cmbyear.Value
Cells(selectedRow, 5).Value = UNIT.Value
Cells(selectedRow, 6).Value = PCBOX.Value
Cells(selectedRow, 7).Value = WASTE.Value
Cells(selectedRow, 8).Value = REPORTED.Value
Cells(selectedRow, 9).Value = DETBOX.Value
Cells(selectedRow, 10).Value = FOLBOX.Value
Cells(selectedRow, 11).Value = SUMBOX.Value
Cells(selectedRow, 12).Value = CAPBOX.Value
Cells(selectedRow, 13).Value = EHOR.Value
Cells(selectedRow, 14).Value = TECHS.Value & "," & TECHS2.Value & "," & TECHS3.Value & "," & TECHS4.Value
Cells(selectedRow, 15).Value = ERRORBOX.Value
Cells(selectedRow, 16).Value = PREVBOX.Value
Cells(selectedRow, 17).Value = SOP.Value
Cells(selectedRow, 18).Value = AUDIFILE.Value
Cells(selectedRow, 19).Value = INTERFILE.Value
Cells(selectedRow, 20).Value = cmbdate2.Value & "/" & cmbmonth2.Value & "/" & cmbyear2.Value
Cells(selectedRow, 23).Value = Phase.Value
Cells(selectedRow, 24).Value = QIM.Value
MsgBox "Entry updated, please check your entry.", , "Entry Update"
End Sub
The problem seems to occur with the lines for columns, 16, 20 and 23 (which refer to the PREVBOX, dates, and Phase values respectively. Attached below is an image of the stray cells being copied to Sheet1 cells highlighted in yellow (and effectively overwriting any cells on that sheet).
The rows and columns also match the rows and columns of the data on Sheet2 but I'm not sure if the cells are being overwritten in Sheet2 which is the intention of the update button.

how to loop through a wild card search with multiple matches. my code works but only finds 1st match from each sheet

Sub find_match_engine()
Dim mykeyword As String
Dim foundRange As Range
Dim LastRow As Long, ws As Worksheet
Dim Row As Variant
Dim Name As String
mykeyword = ThisWorkbook.Sheets("Search").Range("L2").Value
ThisWorkbook.Sheets("Search").Range("A3:K365").ClearContents
Application.ScreenUpdating = False
Set foundRange = ThisWorkbook.Sheets("Denyo").Range("A3:A60").Find(mykeyword & "*")
If foundRange Is Nothing Then
GoTo Line1
Exit Sub
Else
'While foundRange <> ""
Set ws = ThisWorkbook.Sheets("Search")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("A" & LastRow).Value = "Denyo"
ws.Range("A" & LastRow).Offset(0, 1).Value = foundRange.Value
ws.Range("A" & LastRow).Offset(0, 2).Value = foundRange.Offset(0, 1).Value
ws.Range("A" & LastRow).Offset(0, 3).Value = foundRange.Offset(0, 2).Value
ws.Range("A" & LastRow).Offset(0, 4).Value = foundRange.Offset(0, 3).Value
ws.Range("A" & LastRow).Offset(0, 5).Value = foundRange.Offset(0, 4).Value
ws.Range("A" & LastRow).Offset(0, 6).Value = foundRange.Offset(0, 5).Value
ws.Range("A" & LastRow).Offset(0, 7).Value = foundRange.Offset(0, 6).Value
ws.Range("A" & LastRow).Offset(0, 8).Value = foundRange.Offset(0, 7).Value
ws.Range("A" & LastRow).Offset(0, 9).Value = foundRange.Offset(0, 8).Value
ws.Range("A" & LastRow).Offset(0, 10).Value = foundRange.Offset(0, 9).Value
'Wend
End If
Line1:
Set foundRange = ThisWorkbook.Sheets("Hitachi").Range("A3:A358").Find(mykeyword & "*")
If foundRange Is Nothing Then
GoTo Line2
'MsgBox "No Engine Model Files Found", vbInformation, "NO FILE HISTORY"
Exit Sub
Else
'While Name <> ""
Set ws = ThisWorkbook.Sheets("Search")
LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range("A" & LastRow).Value = "Hitachi"
ws.Range("A" & LastRow).Offset(0, 1).Value = foundRange.Value
ws.Range("A" & LastRow).Offset(0, 2).Value = foundRange.Offset(0, 1).Value
ws.Range("A" & LastRow).Offset(0, 3).Value = foundRange.Offset(0, 2).Value
ws.Range("A" & LastRow).Offset(0, 4).Value = foundRange.Offset(0, 3).Value
ws.Range("A" & LastRow).Offset(0, 5).Value = foundRange.Offset(0, 4).Value
ws.Range("A" & LastRow).Offset(0, 6).Value = foundRange.Offset(0, 5).Value
ws.Range("A" & LastRow).Offset(0, 7).Value = foundRange.Offset(0, 6).Value
ws.Range("A" & LastRow).Offset(0, 8).Value = foundRange.Offset(0, 7).Value
ws.Range("A" & LastRow).Offset(0, 9).Value = foundRange.Offset(0, 8).Value
ws.Range("A" & LastRow).Offset(0, 10).Value = foundRange.Offset(0, 9).Value
'Wend
End If
Line2:

Faster Loop with VLOOKUP from another workbook

The code takes information from another range of cells in the same sheet ("Non Order RAW Detail Report") and creates a new one beside it using data from the one to the left.
My code takes from 45 minutes to an hour for 70k rows of cells. Is there a way to make the loops go faster?
Sub cruzar()
Dim i As Long
Dim last As Long
Dim user As String
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
Sheets("Non Order RAW Detail Report").Select
last = ActiveSheet.UsedRange.Rows.Count
'-------------Cruzar--------------------------------
Range("W1").Value = "Year"
Range("x1").Value = "Month"
Range("y1").Value = "Creation Date"
Range("z1").Value = "Closed Date"
Range("AA1").Value = "Type Inquiry"
Range("AB1").Value = "Value"
Range("AC1").Value = "Status"
Range("AD1").Value = "Equal Month"
Range("AE1").Value = "Days"
Range("AF1").Value = "Bracket"
Range("W1:AF1").Interior.ColorIndex = 49
Range("W1:AF1").Font.Color = vbWhite
Range("W1:AF1").Font.Bold = True
user = Environ("username")
For i = 2 To last
Cells(i, 23).Value = Cells(i, 15).Value
Cells(i, 23).NumberFormat = "yyyy"
Cells(i, 24).Value = Cells(i, 15).Value
Cells(i, 24).NumberFormat = "mm"
Cells(i, 25).Value = Cells(i, 15).Value
Cells(i, 25).NumberFormat = "mm/dd/yyyy"
Cells(i, 26).Value = Cells(i, 16).Value
Cells(i, 26).NumberFormat = "mm/dd/yyyy"
Cells(i, 27).Formula = "=VLOOKUP(N" & i & ",'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[CategoryList_NAM_v2.xlsx]CATEGORIES'!$I:$J,2,0)"
Cells(i, 28) = 1
If Cells(i, 5).Value = "FCR" Then
Cells(i, 29).Value = "FCR"
Else
Cells(i, 29).Value = "Follow Up"
End If
Cells(i, 30).Formula = "=IF(E" & i & "=""FCR"",""FCR"",IF(AND(MONTH(O" & i & ")=MONTH(P" & i & "),YEAR(O" & i & ")=YEAR(P" & i & ")),""Closed"",""Open""))"
Cells(i, 31).Formula = "=IF(E" & i & "=""FCR"",""FCR"",IF(AD" & i & "=""Open"",""Open"",IF(((P" & i & "-O" & i & ")*24)<24,0,LOOKUP(((P" & i & "-O" & i & ")*24),'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[1610_InputsDB.xlsx]Input'!$A$2:$B$366,'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[1610_InputsDB.xlsx]Input'!$C$2:$C$366))))"
Cells(i, 32).Formula = "=IF(AE" & i & "=""FCR"",""FCR"",IF(AE" & i & "=""Open"",""Open"",LOOKUP(AE" & i & ",'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[1610_InputsDB.xlsx]Input'!$E$2:$F$9,'C:\Users\" & user & "\Desktop\Americas\00 Inputs\[1610_InputsDB.xlsx]Input'!$G$2:$G$9)))"
Next
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Runtime Error: 1004 : Save as method of worksheet class failed on MAC

This is MAC specific issue, on WindowsOS, it works absolutely fine.
In this macro, I am fetching values from certain cells (from Sheet 1) & then copying this concatenated text to Sheet 3 of this spreadsheet.
While executing the code on MAC having Excel 2016, I am receiving the error
RunTime Error 1004: SaveAs method of Worsheet class Failed.
Error code as below:
Worksheets("Sheet3").SaveAs Filename:=path1 & "\" & Worksheets("Sheet1").Cells(28, 2).Value & ".csv", FileFormat:=xlCSV, CreateBackup:=False
Complete code is as below:
Sub Generate()
ThisWorkbook.Worksheets("Sheet1").Unprotect ("laces")
Dim sOutputPath As String
Dim val2 As Variant
Dim lastrow As Long
path1 = ActiveWorkbook.Path
Set oExcel = CreateObject("Excel.Application")
Set Osheet = ActiveWorkbook.Worksheets("Sheet1")
sVal1 = Osheet.Cells(37, 2).Value & Osheet.Cells(37, 3).Value & Osheet.Cells(37, 4).Value & ";" 'Reference Mapping
sVal1 = sVal1 & Osheet.Cells(2, 9).Value & ";" 'Date Mapping
sVal1 = sVal1 & Osheet.Cells(29, 2).Value & Osheet.Cells(29, 3).Value & Osheet.Cells(29, 4).Value & ";" 'Name Mapping
sVal1 = sVal1 & Osheet.Cells(28, 2).Value & Osheet.Cells(28, 3).Value & Osheet.Cells(28, 4).Value & ";" 'Company Mapping
sVal1 = sVal1 & Osheet.Cells(30, 2).Value & Osheet.Cells(30, 3).Value & Osheet.Cells(30, 4).Value & ";" 'Street1 Mapping
sVal1 = sVal1 & "a" & ";" 'Street2 Mapping
sVal1 = sVal1 & Osheet.Cells(31, 2).Value & Osheet.Cells(31, 3).Value & Osheet.Cells(31, 4).Value & ";" 'Zip Code Mapping
sVal1 = sVal1 & Osheet.Cells(32, 2).Value & Osheet.Cells(32, 3).Value & Osheet.Cells(32, 4).Value & ";" 'City Mapping
sVal1 = sVal1 & "" & ";" 'Region Mapping
sVal1 = sVal1 & Osheet.Cells(33, 2).Value & Osheet.Cells(33, 3).Value & Osheet.Cells(33, 4).Value & ";" 'Country Mapping
sVal1 = sVal1 & Osheet.Cells(34, 2).Value & Osheet.Cells(34, 3).Value & Osheet.Cells(34, 4).Value & ";" 'Email Mapping
sVal1 = sVal1 & Osheet.Cells(35, 2).Value & Osheet.Cells(35, 3).Value & Osheet.Cells(35, 4).Value & ";" 'Phone Mapping
hData1 = "Reference;Date;Name;Company;Street1;Street2;Zip Code;City;Region;Country;Email;Phone;Product;SKU;Quantity"
Worksheets("Sheet3").Range("A1").Value = hData1
hData1 = ""
'Product & Quality Mapping
For i = 6 To 13
S = Osheet.Cells(i, 6).Value
M = Osheet.Cells(i, 7).Value
L = Osheet.Cells(i, 8).Value
XL = Osheet.Cells(i, 9).Value
If (S = "" And M = "" And L = "" And XL = "") Then
Else
' SKU Mapping
SKU = Osheet.Cells(i, 1).Value & " - " & Osheet.Cells(i, 2).Value
If S <> "" Then
expKey = Osheet.Cells(i, 2).Value & "S"
sQuantity = Osheet.Cells(i, 6)
val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 6).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 6).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If
If M <> "" Then
expKey = Osheet.Cells(i, 2).Value & "M"
sQuantity = Osheet.Cells(i, 7)
val4 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 7).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 7).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val4 & ";" & sQuantity
val4 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If
If L <> "" Then
expKey = Osheet.Cells(i, 2).Value & "L"
sQuantity = Osheet.Cells(i, 8)
val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 8).Value, 1))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 8).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If
If XL <> "" Then
expKey = Osheet.Cells(i, 2).Value & "XL"
sQuantity = Osheet.Cells(i, 9)
val3 = findSKU((Osheet.Cells(i, 2).Value), Left(Osheet.Cells(5, 9).Value, 2))
rData1 = sVal1 & sProduct & SKU & " " & Application.Trim(Osheet.Cells(5, 9).Value) & " / " & Osheet.Cells(i, 2).Value & ";" & val3 & ";" & sQuantity
val3 = ""
lRow = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Cells(1 + lRow, 1).Value = rData1
rData1 = ""
End If
End If
Next
Worksheets("Sheet3").SaveAs Filename:=path1 & "\" & Worksheets("Sheet1").Cells(28, 2).Value & ".csv", _
FileFormat:=xlCSV, CreateBackup:=False
MsgBox "You can find the generated CSV file in " & path1
Sheet3.Cells.Clear
ActiveWorkbook.Close savechanges:=False
Set oExcel = Nothing
ThisWorkbook.Worksheets("Sheet1").Protect ("laces")
End Sub
Function findSKU(farb As String, size As String) As String
Dim ri As Long
For ri = 2 To 33
If Sheet2.Cells(ri, 1).Value = farb And Sheet2.Cells(ri, 2).Value = size Then
findSKU = Sheet2.Cells(ri, 3).Value
Exit Function
End If
Next ri
End Function
Any help would be highly appreciated.
Many thanks.

How to copy data whilst changing column

Me again... I've got some code that copies cells from a certain column (from sheet "Convertor") and pastes it into a different column (sheet "Unallocated"). These values (IDs) are then used as a reference point to move the rest of the cells for each row (record) into the correct position I need it in.
However I can't get the code to continuously copy the IDs into a blank row so that they don't overwrite the previous set. I think it's something to do with the line Master.Cells(rowB, colB) = yourData but I can't figure it out. I tried changing the rowB to be the same xlUp to find the last unused cell in the column (as with lastA = Slave.Cells(Rows.Count, colA).End(xlUp).Row), but I couldn't get it to work. Any ideas?
Current code:
Private Sub CommandButton21_Click()
Dim colA As Integer, colB As Integer
Dim rowA As Integer, rowB As Integer
Dim Master As Worksheet, Slave As Worksheet 'declare both
Application.ScreenUpdating = False
Set Master = ThisWorkbook.Worksheets("Unallocated")
Set Slave = ThisWorkbook.Worksheets("Convertor")
colA = 17
colB = 29
rowA = 1
rowB = 1
lastA = Slave.Cells(Rows.Count, colA).End(xlUp).Row 'This finds the last row of the data of the column FROM which i'm copying
For x = rowA To lastA 'Loops through all the rows of A
yourData = Cells(x, colA)
Master.Cells(rowB, colB) = yourData
rowB = rowB + 1 'Increments the current line of destination workbook
Next x 'Skips to next row
For j = 1 To 5000 '(the master sheet)
For i = 1 To 5000 '(the slave sheet) 'for first 1000 cells
If Trim(Master.Cells(j, 29).Value2) = vbNullString Then Exit For 'if ID cell is blank exit
If Master.Cells(j, 29).Value = Slave.Cells(i, 17).Value Then
If IsEmpty(Slave.Cells(i, 3)) Then Exit Sub
Master.Cells(j, 2).Value = Slave.Cells(i, 3).Value 'Move all other data based on the ID
Master.Cells(j, 8).Value = Slave.Cells(i, 4).Value
Master.Cells(j, 9).Value = Slave.Cells(i, 5).Value
Master.Cells(j, 10).Value = Slave.Cells(i, 6).Value
Master.Cells(j, 11).Value = Slave.Cells(i, 7).Value
Master.Cells(j, 12).Value = Slave.Cells(i, 8).Value
Master.Cells(j, 13).Value = Slave.Cells(i, 9).Value
Master.Cells(j, 4).Value = Slave.Cells(i, 10).Value
Master.Cells(j, 23).Value = Slave.Cells(i, 11).Value
Master.Cells(j, 24).Value = Slave.Cells(i, 12).Value
Master.Cells(j, 25).Value = Slave.Cells(i, 13).Value
Master.Cells(j, 26).Value = Slave.Cells(i, 14).Value
Master.Cells(j, 27).Value = Slave.Cells(i, 15).Value
Master.Cells(j, 28).Value = Slave.Cells(i, 16).Value
If Not IsEmpty(Slave.Cells(i, 3)) Then _
Slave.Cells(i, 3).EntireRow.Delete 'deletes row after it has been copied
End If
Next
Next
Application.ScreenUpdating = True
End Sub
Let's start with a simple loop copying data for each row. Then you can add in your checks.
You can use worksheet.range to write to cells (column row) such as ("A4") or ("A" & counter).
Private Sub CommandButton21_Click()
Dim ws As Excel.Worksheet
Dim wsMaster As Excel.Worksheet
Dim strValue As String
Set ws = ActiveWorkbook.Sheets("Convertor")
Set wsMaster = ActiveWorkbook.Sheets("Unallocated")
'Count of row to read from
Dim lRow As Long
lRow = 1
'Count of row to write to
Dim jRow As Long
jRow = 1
ws.Activate
'Loop through and copy what is in the rows
Do While lRow <= ws.UsedRange.Rows.count
wsMaster.Range("AC" & jRow).Value = ws.Range("Q" & lRow).Value
wsMaster.Range("B" & jRow).Value = ws.Range("C" & lRow).Value
wsMaster.Range("H" & jRow).Value = ws.Range("D" & lRow).Value
wsMaster.Range("I" & jRow).Value = ws.Range("E" & lRow).Value
wsMaster.Range("J" & jRow).Value = ws.Range("F" & lRow).Value
wsMaster.Range("K" & jRow).Value = ws.Range("G" & lRow).Value
wsMaster.Range("L" & jRow).Value = ws.Range("H" & lRow).Value
wsMaster.Range("M" & jRow).Value = ws.Range("I" & lRow).Value
wsMaster.Range("D" & jRow).Value = ws.Range("J" & lRow).Value
wsMaster.Range("W" & jRow).Value = ws.Range("K" & lRow).Value
wsMaster.Range("X" & jRow).Value = ws.Range("L" & lRow).Value
wsMaster.Range("Y" & jRow).Value = ws.Range("M" & lRow).Value
wsMaster.Range("Z" & jRow).Value = ws.Range("N" & lRow).Value
wsMaster.Range("AA" & jRow).Value = ws.Range("O" & lRow).Value
wsMaster.Range("AB" & jRow).Value = ws.Range("P" & lRow).Value
ws.Rows(lRow).EntireRow.Delete
'Increment counters for both sheets. We can actually use just one counter, but if there is ever a condition that will cause us to not copy a row, then we will need two counters.
jRow = jRow + 1
'lRow = lRow + 1 'This is commented out because we are deleting rows after we copy them.
Loop
End Sub
If you really need to delete the rows after they are copied then we will have to not increment the lRow value.
.Cells is Limiting your approach.
Consider Change to Using Range("A1:C3000") notation it's more powerful.
Range.Select
Range.Paste (to new High mark for UsedRows.Count at destination)
Also unless you have exactly 5000 rows, it's not that accurate,
experiment with
ActiveSheet.UsedRange.Rows.Count

Resources