How to copy from column to row not using PasteSpecial Transpose? - vba

This loop to copies the values from one sheet's columns to another sheet's columns:
Dim ExposureDataInput As Worksheet
Dim ManualSimulation As Worksheet
Set EDI = Sheets("ExposureDataInput")
Set MS = Sheets("ManualSimulation")
Dim i As Integer
Dim n As Integer
For i = 2 To EDI.Range("B" & Rows.Count).End(xlUp).Row
If EDI.Range("B" & i).Value > 0 Then
n = MS.Range("A" & Rows.Count).End(xlUp).Row + 1
MS.Range("A" & n).Value = EDI.Cells(i, 1).Value
n = MS.Range("B" & Rows.Count).End(xlUp).Row + 1
MS.Range("B" & n).Value = EDI.Cells(i, 2).Value
n = MS.Range("C" & Rows.Count).End(xlUp).Row + 1
MS.Range("C" & n).Value = EDI.Cells(i, 4).Value
n = MS.Range("D" & Rows.Count).End(xlUp).Row + 1
MS.Range("D" & n).Value = EDI.Cells(i, 6).Value
n = MS.Range("E" & Rows.Count).End(xlUp).Row + 1
MS.Range("E" & n).Value = EDI.Cells(i, 8).Value
n = MS.Range("F" & Rows.Count).End(xlUp).Row + 1
MS.Range("F" & n).Value = EDI.Cells(i, 10).Value
n = MS.Range("G" & Rows.Count).End(xlUp).Row + 1
MS.Range("G" & n).Value = EDI.Cells(i, 12).Value
End If
Next i
I tried the same principal to get the col A:A from one sheet to a row in another sheet:
Dim ExposureDataInput As Worksheet
Dim HistoricalDataandExcessReturns As Worksheet
Set EDI = Sheets("ExposureDataInput")
Set HDaER = ThisWorkbook.Worksheets("HistoricalDataandExcessReturns")
Dim k As Integer
Dim y As Integer
For k = 2 To EDI.Range("B" & Rows.Count).End(xlUp).Row
If EDI.Range("B" & k).Value > 0 Then
y = HDaER.Range(Columns.Count & 1).End(xlToLeft).Column + 1
HDaER.Range(y & 1).Value = EDI.Cells(1, k).Value
y = HDaER.Range(Columns.Count & 2).End(xlToLeft).Column + 1
HDaER.Range(y & 2).Value = EDI.Cells(2, k).Value
End If
Next k
The i in the column to column works. When I try with k in a column to row it gives me
Run-time error '1004'.
How can I copy a column to a row?

I believe the issue lies with the way you are trying to get the last Column, please have a look at my answer below:
The first sub could be written as:
Dim ExposureDataInput As Worksheet
Dim ManualSimulation As Worksheet
Set EDI = Sheets("ExposureDataInput")
Set MS = Sheets("ManualSimulation")
Dim i As Long
Dim n As Long
For i = 2 To EDI.Range("B" & EDI.Rows.Count).End(xlUp).Row
If EDI.Range("B" & i).Value > 0 Then
n = MS.Cells(MS.Rows.Count, "A").End(xlUp).Row + 1
'get the next free row without data on Column A
MS.Range("A" & n).Value = EDI.Cells(i, 1).Value
MS.Range("B" & n).Value = EDI.Cells(i, 2).Value
MS.Range("C" & n).Value = EDI.Cells(i, 4).Value
MS.Range("D" & n).Value = EDI.Cells(i, 6).Value
MS.Range("E" & n).Value = EDI.Cells(i, 8).Value
MS.Range("F" & n).Value = EDI.Cells(i, 10).Value
MS.Range("G" & n).Value = EDI.Cells(i, 12).Value
End If
Next i
The second sub could be written as:
Dim ExposureDataInput As Worksheet
Dim HistoricalDataandExcessReturns As Worksheet
Set EDI = Sheets("ExposureDataInput")
Set HDaER = ThisWorkbook.Worksheets("HistoricalDataandExcessReturns")
Dim k As Long
Dim y As Long
For k = 1 To EDI.Cells(EDI.Rows.Count, "B").End(xlUp).Row
If EDI.Range("B" & k).Value > 0 Then
y = HDaER.Cells(1, HDaER.Columns.Count).End(xlToLeft).Column + 1
'count the number of column on row 1
HDaER.Cells(1, y).Value = EDI.Cells(k, 1).Value
y = HDaER.Cells(2, HDaER.Columns.Count).End(xlToLeft).Column + 1
'count the number of columns on row 2?
HDaER.Cells(2, y).Value = EDI.Cells(k, 2).Value
End If
Next k

Actually, the error you should be getting is 6 - Overflow.
Try this small piece of code:
Sub TestMe()
Dim a As Integer
a = Rows.Count
End Sub
You would get an overflow error, because the Integer is from -32768 to 32767 and the rows in Excel are more than 1 million. The columns are 16384, thus they enough for an integer.
Replace the Integer with Long and try again.

Related

Counting the columns in sheet1 and pasting their result in a table in sheet 2

I have two sheets. sheet1 is Evaluation and sheet2 is the result sheet.
From the Evaluation sheet1, I would like to count the number of "OK" in Column S and T and count the number of "Invalid" in the column R.
These counted values are to be entered In the sheet2 result , in their corresponding columns.
I was working with this code and It worked completely fine.
But now, when I run the code, number of OK in the column S and T and number of Invalid in the column R are not counted.
Here is the Code I am trying to work with
Sub result()
Dim i As Integer
Dim j As Integer
Dim cnt As Integer
Dim cntU, CntS, cntT As Integer
Dim sht As Worksheet
Dim totalrows, n As Long
Set sht = Sheets("Result")
Sheets("Evaluation").Select
totalrows = Range("A5").End(xlDown).Row
n = Worksheets("FC_SCR Evaluation").Range("A5:A" & totalrows).Cells.SpecialCells(xlCellTypeConstants).count
For i = 2 To WorksheetFunction.count(sht.Columns(1))
cntT = 0
cntU = 0
CntS = 0
' get the current week of column A of result sheet
If sht.Range("A" & i) = Val(Format(Now, "WW")) Then Exit For
Next i
' if column A of result sheet and Column x of fc sheet are same, then count the mentioned parameters
For j = 5 To WorksheetFunction.CountA(Columns(23))
If sht.Range("A" & i) = Range("X" & j) And Range("T" & j) = "OK" Then cntT = cntT + 1
If sht.Range("A" & i) = Range("X" & j) And Range("S" & j) = "OK" Then cntU = cntU + 1
If sht.Range("A" & i) = Range("X" & j) And Range("R" & j) = "Invalid" Then CntS = CntS + 1
' print the counted value in corresponding column of result sheet
If cntT <> 0 Then sht.Range("D" & i) = cntT
If cntU <> 0 Then sht.Range("E" & i) = cntU
If CntS <> 0 Then sht.Range("C" & i) = CntS
If n <> 0 Then sht.Range("B" & i) = n
Next j
If cntT + cntU <> 0 Then
sht.Range("F" & i) = CntS / n
sht.Range("g" & i) = cntT / n
sht.Range("h" & i) = cntU / n
End If
End Sub

Counting the value and pasting in another sheet based on the Cases

I have sheet "BW" and another sheet "Result". The sheet result has an Table with the weeknumber in column A.
I wanted to look for the weeknumber in sheet "result" and if the weeknumber is the same as the Weeknumber in my sheet "BW" in column "AX" then , I would like to count the number of "1" in the sheet "BW" in column "T" and "u".
But the problem is I have two cases for the counting of "1" in Column T and U
The column AA should not be equal to null.
The column Z should Contain "Ontime".
If these two case satisfies, then I would like to look into the column T and U and wount count for the 1 in the column.
I tried the code. But the case is not getting satisfied. Any lead would be helpful.
Sub results()
Dim i As Integer, j As Integer, cntT As Integer, cntu As Integer, ws As Worksheet
Set ws = Sheets("Result")
Sheets("BW").Select
For i = 2 To WorksheetFunction.CountA(ws.Columns(1))
cntT = 0
cntu = 0
If ws.Range("A" & i) = Val(Format(Now, "ww")) Then Exit For
Next i
For j = 5 To WorksheetFunction.CountA(Columns(50))
If ws.Range("AA" & i) <> "" And ws.Range("Z" & i) = "PSW Ontime" Then
ElseIf ws.Range("A" & i) = Range("AX" & j) And Range("T" & j) = 1 Then cntT = cntT + 1
ElseIf ws.Range("A" & i) = Range("AX" & j) And Range("U" & j) = 1 Then cntu = cntu + 1
Next j
If cntT <> 0 Then ws.Range("B" & i) = cntT
If cntu <> 0 Then ws.Range("C" & i) = cntu
If cntT + cntu <> 0 Then
ws.Range("D" & i) = cntT + cntu
ws.Range("E" & i) = cntT / (cntT + cntu)
ws.Range("F" & i) = cntu / (cntT + cntu)
End If
ws.Range("E" & i & ":F" & i).NumberFormat = "0%"
End If
End Sub
Could someone help me , how I can introduce this case in my code.

check for empty cel and then count the value in the column and paste it to another sheet

I am having two sht, sht1 as BW and sht2 as result.
I want to Count the number of 1's in column T and U of sht1, according to the week in column AX and print the Counted values in the result sheet, looking into the same week.
I took the advice of an expert in the Forum and completed till this.
I want an if condition, in such a way that, if the column AA is empty, then i should not Count the 1's in column T and U, it should be skipped.
I introduced this code of line in my existing code after j=2; and got an error
error Label not defined`,
If ws.Range("AA" & i) = "" Then
GoTo nextrow
could someone help, how i should execute this condition ?
here is my code;
Sub results()
Dim i As Integer, j As Integer, cntT As Integer, cntu As Integer, ws As Worksheet
Set ws = Sheets("Result")
Sheets("BW").Select
For i = 2 To WorksheetFunction.CountA(ws.Columns(1))
cntT = 0
cntu = 0
If ws.Range("A" & i) = Val(Format(Now, "ww")) Then Exit For
Next i
For j = 2 To WorksheetFunction.CountA(Columns(50))
If ws.Range("A" & i) = Range("AX" & j) And Range("T" & j) = 1 Then cntT = cntT + 1
If ws.Range("A" & i) = Range("AX" & j) And Range("U" & j) = 1 Then cntu = cntu + 1
Next j
If cntT <> 0 Then ws.Range("B" & i) = cntT
If cntu <> 0 Then ws.Range("C" & i) = cntu
If cntT + cntu <> 0 Then
ws.Range("D" & i) = cntT + cntu
ws.Range("E" & i) = cntT / (cntT + cntu)
ws.Range("F" & i) = cntu / (cntT + cntu)
End If
ws.Range("E" & i & ":F" & i).NumberFormat = "0%"
End Sub
The GoTo nextrow is looking for a line label titled nextrow. That's how the GoTo command is built and operates. For what you are trying to accomplish, try this instead:
If ws.Range("AA" & i) <> "" Then
If ws.Range("A" & i) = Range("AX" & j) And Range("T" & j) = 1 Then cntT = cntT + 1
If ws.Range("A" & i) = Range("AX" & j) And Range("U" & j) = 1 Then cntu = cntu + 1
End If
This way it says, if the column is NOT blank, then do stuff, otherwise go to next j
If you want to use the GoTo Command in the future you can use line numbers (like GoTo 0 which should take you back to the beginning of the code) or you can create a line label by placing nextrow: on an empty line. Then when you call GoTo nextrow it will go to that line, and proceed from there.

Comparing values in two columns using VBA

I am working on this code that compares column A ( code source) and column B( code roc) and for each code source in column A it has his code regate in column C and address in column D so if A=B copy them back in E and F with their code regate in column G and their address in column H .
this the code I am using it blocks until I shut down excel and it doesn't give me the exact results if anyone can help me thank you
here is a picture of the result that i need from A and B , C and D
Sub copy_lignes()
Dim DerLigA, DerLigB As Long, i, j As Long
DerLigA = Sheets("sheet3").Range("A" & Rows.Count).End(xlUp).Row
DerLigB = Sheets("sheet3").Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To DerLigA
For j = 2 To DerLigB
If Sheets("sheet3").Range("A" & i) = Sheets("sheet3").Range("B" & j) Then
Sheets("sheet3").Range("A" & i).Copy Destination:=Sheets("sheet3").Range("E" & i)
Sheets("sheet3").Range("B" & i).Copy Destination:=Sheets("sheet3").Range("F" & i)
Sheets("sheet3").Range("C" & i).Copy Destination:=Sheets("sheet3").Range("G" & i)
Sheets("sheet3").Range("D" & i).Copy Destination:=Sheets("sheet3").Range("H" & i)
End If
Next j
Next i
End Sub
Try the code below, maybe this is what you meant in your post:
Sub copy_lignes()
Dim DerLigA, DerLigB As Long, i, j As Long
Dim PasteRow As Long
' optimize speed performance
Application.ScreenUpdating = False
With Sheets("Sheet3")
DerLigA = .Cells(.Rows.Count, "A").End(xlUp).Row
DerLigB = .Cells(.Rows.Count, "B").End(xlUp).Row
PasteRow = 2
For i = 2 To DerLigA
For j = 2 To DerLigB
If .Range("A" & i) = .Range("B" & j) Then
.Range("A" & i).Copy Destination:=.Range("E" & PasteRow)
.Range("B" & j & ":D" & j).Copy Destination:=.Range("F" & PasteRow)
PasteRow = PasteRow + 1
End If
Next j
Next i
End With
' restore settings
Application.ScreenUpdating = True
End Sub
It might be that you just need to tab in a few lines, so it should look like this:
Sub copy_lignes()
Dim DerLigA As Long
Dim DerLigB As Long
Dim i As Integer
Dim j As Integer
i = 2
j = 2
DerLigA = Sheets("sheet3").Range("A" & Rows.Count).End(xlUp).Row
DerLigB = Sheets("sheet3").Range("B" & Rows.Count).End(xlUp).Row
For i To DerLigA
For j To DerLigB
If Sheets("sheet3").Range("A" & i) = Sheets("sheet3").Range("B" & j) Then
Sheets("sheet3").Range("A" & i).Copy Destination:=Sheets("sheet3").Range("E" & i)
Sheets("sheet3").Range("B" & i).Copy Destination:=Sheets("sheet3").Range("F" & i)
Sheets("sheet3").Range("C" & i).Copy Destination:=Sheets("sheet3").Range("G" & i)
Sheets("sheet3").Range("D" & i).Copy Destination:=Sheets("sheet3").Range("H" & i)
End If
Next j
Next i
End Sub

Reset row increment before going to next worksheet

I have 2 workbooks. One wb acts as a database (divided into users), while the other is used to pull data from that wb based on a date. I looped the macro and at first it seems to pull data only from the first sheet. See NOTE below
Dim y As Integer
Dim r As Integer
Dim WS_Count As Integer
Dim I As Integer
r = CollateSh3.Range("D" & Rows.Count).End(xlUp).Row + 1
y = 3
WS_Count = wbLog.Worksheets.Count
For I = 1 To WS_Count
Do Until wbLog.Sheets(I).Range("A" & y) = ""
If wbLog.Sheets(I).Range("B" & y).Value = RequiredDate Then
CollateSh3.Range("A" & r).Value = wbLog.Sheets(I).Range("D" & y).Value
CollateSh3.Range("C" & r).Value = wbLog.Sheets(I).Range("F" & y).Value
CollateSh3.Range("D" & r).Value = wbLog.Sheets(I).Range("G" & y).Value
CollateSh3.Range("E" & r).Value = wbLog.Sheets(I).Range("H" & y).Value
CollateSh3.Range("F" & r).Value = wbLog.Sheets(I).Range("I" & y).Value
CollateSh3.Range("G" & r).Value = wbLog.Sheets(I).Range("J" & y).Value
CollateSh3.Range("H" & r).Value = wbLog.Sheets(I).Range("K" & y).Value
CollateSh3.Range("I" & r).Value = wbLog.Sheets(I).Range("L" & y).Value
CollateSh3.Range("K" & r).Value = wbLog.Sheets(I).Range("N" & y).Value
r = r + 1
y = y + 1
Else
y = y + 1
End If
Loop
'MsgBox wbLog.Sheets(I).Name
Next I
EDIT: I think I might have useful additional info. It seems that the macro doesn't skip. When I tried to add more entries to the database file, it pulls data, except that it starts at the row where it ended on the previous sheet. For e.g., if it ended on row 9 on sh1, the code starts at row 10 on sh2.
How do I reset the increment before going to the next sheet?
Here's the edited code. I opted to add it after the end of the loop, because that way I'd remember it for future reference. Thanks #PatricK!
Dim y As Integer
Dim r As Integer
Dim WS_Count As Integer
Dim I As Integer
r = CollateSh3.Range("D" & Rows.Count).End(xlUp).Row + 1
y = 3
WS_Count = wbLog.Worksheets.Count
For I = 1 To WS_Count
Do Until wbLog.Sheets(I).Range("A" & y) = ""
If wbLog.Sheets(I).Range("B" & y).Value = RequiredDate Then
CollateSh3.Range("A" & r).Value = wbLog.Sheets(I).Range("D" & y).Value
CollateSh3.Range("C" & r).Value = wbLog.Sheets(I).Range("F" & y).Value
CollateSh3.Range("D" & r).Value = wbLog.Sheets(I).Range("G" & y).Value
CollateSh3.Range("E" & r).Value = wbLog.Sheets(I).Range("H" & y).Value
CollateSh3.Range("F" & r).Value = wbLog.Sheets(I).Range("I" & y).Value
CollateSh3.Range("G" & r).Value = wbLog.Sheets(I).Range("J" & y).Value
CollateSh3.Range("H" & r).Value = wbLog.Sheets(I).Range("K" & y).Value
CollateSh3.Range("I" & r).Value = wbLog.Sheets(I).Range("L" & y).Value
CollateSh3.Range("K" & r).Value = wbLog.Sheets(I).Range("N" & y).Value
r = r + 1
y = y + 1
Else
y = y + 1
End If
Loop
y = 3 'to reset increment before moving on to next sheet
Next I