kiwikiki718
Board Regular
- Joined
- Apr 7, 2017
- Messages
- 80
- Office Version
- 365
- Platform
- Windows
Hi all, I am trying to copy data from one worksheet into another worksheet displaying the status along with the month and score for each status. the code below works just for the for status (High 1) I was wondering how I could continue displaying the results for all the status without having to manually write out each cell. below is a sample table I am working with along with how I want the data to be copied over to sheet 2
sheet 1
sheet 2 ( copying values from sheet 1) how I want the data to display
sheet 1
sheet 2 ( copying values from sheet 1) how I want the data to display
VBA Code:
Sub CopyValues()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim lastRow As Long
' Set references to Worksheet 1 and Worksheet 2
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Set ws2 = ThisWorkbook.Worksheets("Sheet2")
' Copy values from Worksheet 1 to Worksheet 2
ws2.Range("A2").Value = ws1.Range("A2").Value ' Copy value from A2 to A2
ws2.Range("B2").Value = ws1.Range("B1").Value ' Copy value from D1 to B2
ws2.Range("C2").Value = ws1.Range("B2").Value ' Copy value from D2 to D2
' Find the next empty row in column A of Worksheet 2
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
lastRow = IIf(lastRow < 2, 2, lastRow + 1) ' Ensure the minimum starting row is 2
ws2.Range("A" & lastRow).Value = ws1.Range("A2").Value
ws2.Range("B" & lastRow).Value = ws1.Range("C1").Value
ws2.Range("C" & lastRow).Value = ws1.Range("C2").Value
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
lastRow = IIf(lastRow < 2, 2, lastRow + 1)
ws2.Range("A" & lastRow).Value = ws1.Range("A2").Value
ws2.Range("B" & lastRow).Value = ws1.Range("D1").Value
ws2.Range("C" & lastRow).Value = ws1.Range("D2").Value
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
lastRow = IIf(lastRow < 2, 2, lastRow + 1)
ws2.Range("A" & lastRow).Value = ws1.Range("A2").Value
ws2.Range("B" & lastRow).Value = ws1.Range("E1").Value
ws2.Range("C" & lastRow).Value = ws1.Range("E2").Value
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
lastRow = IIf(lastRow < 2, 2, lastRow + 1)
ws2.Range("A" & lastRow).Value = ws1.Range("A2").Value
ws2.Range("B" & lastRow).Value = ws1.Range("F1").Value
ws2.Range("C" & lastRow).Value = ws1.Range("F2").Value
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
lastRow = IIf(lastRow < 2, 2, lastRow + 1)
ws2.Range("A" & lastRow).Value = ws1.Range("A2").Value
ws2.Range("B" & lastRow).Value = ws1.Range("G1").Value
ws2.Range("C" & lastRow).Value = ws1.Range("G2").Value
lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
lastRow = IIf(lastRow < 2, 2, lastRow + 1)
ws2.Range("A" & lastRow).Value = ws1.Range("A2").Value
ws2.Range("B" & lastRow).Value = ws1.Range("H1").Value
ws2.Range("C" & lastRow).Value = ws1.Range("H2").Value
End Sub