Double For Loop Not Increasing - excel

I just can't seem to get this for loop running correctly. I know that I missing something basic, but I just can't figure it.
I have 2 tables.
Table 1 : (Table starts at row 7, and columns i to q are hidden)
Table 2 :
My goal is to pull new rows from Table1 to Table2.
My code rolls through Table 1, identifies the rows with an 'R' value, and fills them.
Then I want to pull data from those same rows to Table2
The code that identifies and fills the 'R' value:
Dim iRow As Long
With Sheet12
iRow = Application.Count(.ListObjects("Table1").ListColumns("KEY").DataBodyRange)
End With
'find last row with a date
Dim jRow As Long
With Sheet12
jRow = Application.Count(.ListObjects("Table1").ListColumns("Date").DataBodyRange)
End With
'take the value from iRow and col 1, add 1, place in iRow+1,1
Dim q As Long
For q = iRow + 7 To jRow + 6
Sheet12.Cells(q, 18) = 1 + Sheet12.Cells(q - 1, 18)
Next q
Then this bit I'm having trouble with. My thought was try to run a double loop, where I loop through to fill each column and then each row.
Dim a As Long
Dim b As Long
Dim c As Long
c = jRow - iRow
For b = 11 To c + 11
For a = iRow + 7 To jRow + 6
ws15.Cells(b, 1).Value = "Plaid-" & Sheet12.Cells(a, 8).Value & "-" & Sheet12.Cells(a, 7).Value
ws15.Cells(b, 2).Value = Sheet12.Cells(a, 18).Value
ws15.Cells(b, 3).Value = Sheet12.Cells(a, 3).Value
ws15.Cells(b, 4).Value = Sheet12.Cells(a, 4).Value
ws15.Cells(b, 5).Value = Sheet12.Cells(a, 5).Value
ws15.Cells(b, 6).Value = 1001
ws15.Cells(b, 7).Value = "FILL IN"
Next a
Next b
Now the above code only copies the last row from Table1 into Table2 four times.
I know I'm close, and I'm sure I'm just tired, but I can't get it right. I appreciate everyone's time.

The double loop is causing the problem. The inside loop fills in the same row 4 times. This explains why every row has the same data.
You want to iterate the rows together so you just need 1 loop. The b variable is not needed.
Try this code:
Dim a As Long
Dim c As Long
c = jRow - iRow + 7 'start row on new sheet
For a = iRow + 7 To jRow + 6 'source data rows
ws15.Cells(c, 1).Value = "Plaid-" & Sheet12.Cells(a, 8).Value & "-" & Sheet12.Cells(a, 7).Value
ws15.Cells(c, 2).Value = Sheet12.Cells(a, 18).Value
ws15.Cells(c, 3).Value = Sheet12.Cells(a, 3).Value
ws15.Cells(c, 4).Value = Sheet12.Cells(a, 4).Value
ws15.Cells(c, 5).Value = Sheet12.Cells(a, 5).Value
ws15.Cells(c, 6).Value = 1001
ws15.Cells(c, 7).Value = "FILL IN"
c = c + 1 'next row on new sheet
Next a

Related

Appreciate any suggestion

I am new to VBA and I will need a help.
I have a worksheet named "Jobs" with raw data table and I want to copy paste certain cells to another worksheet named "Schedule" provided that the source and destination date matches and I use the below. But, I have 3 jobs for the same date and it copy only one. Any help will be appreciated.
Sub CopyBasedonSheet1()
Dim i As Long
Dim j As Long
Worksheets("Schedule").Range("B1:AJ92").ClearContents
Sheet1LastRow = Worksheets("Jobs").Range("G" & Rows.Count).End(xlUp).Row 'G is the Date Column'
Sheet2LastRow = Worksheets("Schedule").Range("A" & Rows.Count).End(xlUp).Row 'A is the Date column'
For j = 1 To Sheet1LastRow
For i = 1 To Sheet2LastRow
If Worksheets("Jobs").Cells(j, 7).Value = Worksheets("Schedule").Cells(i, 1).Value And Worksheets("Jobs").Cells(j, 1).Value = "P" Then
Worksheets("Schedule").Cells(i, 2).Value = Worksheets("Jobs").Cells(j, 3).Value
Worksheets("Schedule").Cells(i, 3).Value = Worksheets("Jobs").Cells(j, 9).Value
Worksheets("Schedule").Cells(i, 4).Value = Worksheets("Jobs").Cells(j, 14).Value
End If
Next i
Next j
End Sub

How to add values in a column?

Sub Formatted_Salary()
Dim lastrow, Total As Integer
lastrow = 0
Total = 0
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = lastrow To 1 Step -1
If Cells(i, 1) = "" Then
ActiveSheet.Rows(i).EntireRow.Delete
ElseIf Cells(i, 11).Value Then
Total = Total + Cell.Value (Problem Area)
End If
Next
newlastrow = Cells(Rows.Count, 1).End(xlUp).Row
Cells(newlastrow + 1, 10).Value = "Total Base Salary"
Cells(newlastrow + 1, 11).Value = Total
Cells(newlastrow + 1, 11).Font.Color = vbGreen
End Sub
The line of code label as problem area is not working, and I would like some help. (im new to all is this). Basically what I would like the code to do is to delete all the blank rows and then add the total value of a column K and the print value at desired location. Thanks. Any help is appreciated.

Excel: How do I 'gather' values to display in another cell

I have two columns, Column A has a set of a few standard values and column B has all unique values. I'm only just experimenting with more complex ways of compiling data than the beginner level so I'm a bit at a loss.
I need to either have a lookup or create a macro that will list only the values in A (once each) but also display which values in B correspond to those in A
for example
A | B
va1|abc
va1|bcd
Va2|xyz
va3|zab
will show (in a single cell) the following
va1: abc, bcd
va2: xyz
va3: zab
Please help!
Option Explicit
Sub Test()
Dim i As Long, j As Long, k As Long
k = 1
For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Application.CountIf(Range("C:C"), Cells(i, 1).Value) = 0 Then
Cells(k, 3).Value = Cells(i, 1).Value
For j = 1 To Cells(Rows.Count, 1).End(xlUp).Row
If Cells(j, 1).Value = Cells(k, 3).Value And _
InStr(Cells(k, 4).Value, Cells(j, 2).Value) = 0 Then
If Cells(k, 4).Value = "" Then
Cells(k, 4).Value = Cells(j, 2).Value
Else
Cells(k, 4).Value = Cells(k, 4).Value & ", " & Cells(j, 2).Value
End If
End If
Next j
k = k + 1
End If
Next i
For i = 1 To Cells(Rows.Count, 3).End(xlUp).Row
Cells(i, 3).Value = Cells(i, 3).Value & ": " & Cells(i, 4).Value
Cells(i, 4).ClearContents
Next i
End Sub
Edited for single cell
In case your requirement is to "have the grouped data", and not exactly "have one single string per A", you can do this with a "pivot table" putting A and B in the row labels, like in the following picture:

Finding Rows with same value and contracting cells

I'm trying to find all rows in a single column with the same value. The program should delete all rows that occur multiple times, apart from one of the columns, which should contract all statements from the deleted rows. This is what I have so far, but I'm getting a loop error:
Sub tester()
Sheets("Sheet1").Select
Dim one As Integer
one = 2
Dim log As Integer
log = 2
Dim compare As Integer
compare = one + 1
Dim ws As String
ws = "Sheet1"
Dim ender As String
ender = "Sheet4"
Dim counter As Integer
counter = 0
Dim lastrow As Integer
lastrow = Cells(Rows.Count, 1).End(xlUp).row
For log = 2 To lastrow - 1
one = log + counter
compare = one + 1
If Worksheets(ws).Cells(one, 1).Value = Worksheets(ws).Cells(compare,1).Value And Worksheets(ws).Cells(one, 7).Value = Worksheets(ws).Cells(compare, 7).Value Then
Do While Worksheets(ws).Cells(one, 1).Value = Worksheets(ws).Cells(compare, 1).Value And Worksheets(ws).Cells(one, 7).Value = Worksheets(ws).Cells(compare, 7).Value
If compare = one + 1 Then
Worksheets(ender).Cells(log, 1).Value = Worksheets(ws).Cells(one, 1).Value
Worksheets(ender).Cells(log, 4).Value = Worksheets(ws).Cells(one, 4).Value
Worksheets(ender).Cells(log, 2).Value = Worksheets(ws).Cells(one, 2).Value
Worksheets(ender).Cells(log, 7).Value = Worksheets(ws).Cells(one, 7).Value
End If
Worksheets(ender).Cells(log, 4).Value = Worksheets(ender).Cells(log, 4).Value & "; " & Worksheets(ws).Cells(compare, 4).Value
compare = compare + 1
counter = counter + 1
Loop
ElseIf Worksheets(ws).Cells(one, 1).Value <> Worksheets(ws).Cells(compare, 1).Value Then
Worksheets(ender).Cells(one - counter, 1).Value = Worksheets(ws).Cells(one, 1).Value
Worksheets(ender).Cells(one - counter, 2).Value = Worksheets(ws).Cells(one, 2).Value
Worksheets(ender).Cells(one - counter, 3).Value = Worksheets(ws).Cells(one, 3).Value
Worksheets(ender).Cells(one - counter, 4).Value = Worksheets(ws).Cells(one, 4).Value
Worksheets(ender).Cells(one - counter, 5).Value = Worksheets(ws).Cells(one, 5).Value
Worksheets(ender).Cells(one - counter, 7).Value = Worksheets(ws).Cells(one, 7).Value
End If
Next log
Sheets("Sheet4").Select
End Sub
Original Data
Desired output

VBA Dynamic Range with Values Calculation

Hello I have the following problem:
As you can see in column A we have Dates, in column B there is always a "1" when a year changes from one to the next, I marked it in yellow. In column H are different values and in column I, I want to have only the FIRST value, which is greater (in this case) than 10% within one year (so in the period from one to one in column B). After that I want to have the next first value, which is >10% in the next year so next period from 1 to 1 in column B and so on.
Can anyone help me?
So far I programmed this, but it shows me all values >10% but not the first from each range to each range.
Sub ABC ()
With ThisWorkbook.Worksheets("Test")
rowCount = 2
Do While .Cells(rowCount + 1, 8).Value <> ""
If .Cells(rowCount, 2).Value = 0 And .Cells(rowCount, 8).Value >= 0.1 Then
.Cells(rowCount, 9).Value = .Cells(rowCount, 7).Value * 0.1 / 1.1
Else
.Cells(rowCount, 9).Value = ""
End If
rowCount = rowCount + 1
Loop
End With
End Sub
It seem the code is error on the cell address
Sub ABC ()
With ThisWorkbook.Worksheets("Test")
rowCount = 2
Do While .Cells(rowCount + 1, 8).Value <> ""
If .Cells(rowCount, 2).Value = 0 And .Cells(rowCount, 8).Value >= 0.1 Then
.Cells(rowCount, 9).Value = .Cells(rowCount, 8).Value * 0.1 / 1.1
Else
.Cells(rowCount, 9).Value = ""
End If
rowCount = rowCount + 1
Loop
End With
End Sub

Resources