I have seen a previous post about this, and have tried to apply it but i have been unsuccesful.
Sub test()
Dim i As Long
Dim varray As Variant
Sheets("Original").Select
varray = Sheets("Original").Range("A10:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value
For i = 10 To UBound(varray, 1)
If Cells(i, 16).Value <> "" Then
Cells(i + 1, 16).EntireRow.Insert
Cells(i + 1, 1).EntireRow.Value = Cells(i, 1).EntireRow.Value
Cells(i + 1, 6).Value = Cells(i, 16).Value
Cells(i + 1, 1).Value = 20305
Cells(i + 1, 11).Value = ""
Cells(i + 1, 12).Value = ""
Cells(i + 1, 15).Value = ""
Cells(i + 1, 16).Value = ""
End If
Next
End Sub
It skips the whole for Loop and goes to End Sub. Any assistance?
Thanks
There are a few problems with the code you have there. I have tried to address them in my script below. Unfortunately there is no example of your data you working with. Have a look and let me know if something is not working.
Option Explicit
Sub test()
'Get used to declaring your worksheet
'and then reference that worksheet when wanting to access data form it.
Dim OrigSht As Worksheet
Set OrigSht = ThisWorkbook.Sheets("Original")
Dim LastRowColA As Long
LastRowColA = OrigSht.Cells(Rows.Count, "A").End(xlUp).Row
'Not sure why you wanted to use an Array, seeing as you dont use it in the loop.
'Unless you use it in some other code and this is a extract from other code.
Dim varray As Variant
varray = OrigSht.Range("A10:A" & LastRowColA).Value
'Using UBound could present errors if there was less than 10 lines of data _
it would then step the loop because the to value is less than the start
'Rather use the last row of column A as a end of the For loop
'The problem with editing a list of data form the begining of a list _
is that the list becomes longer as you add information, so when adding _
or deleting lines you always want to start at the end og the list
Dim i As Long
For i = LastRowColA To 10 Step -1
With OrigSht
If .Cells(i, 16).Value <> "" Then
.Cells(i + 1, 16).EntireRow.Insert
.Cells(i + 1, 1).EntireRow.Value = .Cells(i, 1).EntireRow.Value
.Cells(i + 1, 6).Value = .Cells(i, 16).Value
.Cells(i + 1, 1).Value = 20305
.Cells(i + 1, 11).Value = ""
.Cells(i + 1, 12).Value = ""
.Cells(i + 1, 15).Value = ""
.Cells(i + 1, 16).Value = ""
End If
End With
Next
End Sub
Related
The section that says: RowCount = Cells(Rows.Count, "A").End(x1Up).Row, is highlighted in red and yellow when I select debug. I'm not sure what that is supposed to mean.
Sub DQAnalysis()
Worksheets("DQ Analysis").Activate
Range("A1").Value = "DAQ0 (Ticker: DQ)"
'Create a header row
Cells(3, 1).Value = "Year"
Cells(3, 2).Value = "Total Daily Volume"
Cells(3, 3).Value = "Return"
Worksheets("2018").Activate
'set initial volume to zero
totalVolume = 0
Dim startingPrice As Double
Dim endingPrice As Double
'find the number of rows to loop over
RowCount = Cells(Rows.Count, "A").End(x1Up).Row
'loop over all the rows
For i = 2 To RowCount
If Cells(i, 1).Value = "DQ" Then
'increase totalVolume by the value in the current row
totalVolume = totalVolume + Cells(i, 8).Value
End If
If Cells(i - 1, 1).Value <> "DQ" And Cells(i, 1).Value = "DQ" Then
endingPrice = Cells(i, 6).Value
End If
If Cells(i + 1, 1).Value <> "DQ" And Cells(i, 1).Value = "DQ" Then
endingPrice = Cells(i, 6).Value
End If
Next i
Worksheets("DQ Analysis").Activate
Cells(4, 1).Value = 2018
Cells(4, 2).Value = totalVolume
Cells(4, 3).Value = (endingPrice / startingPrice) - 1
End Sub
This line calculates the last row's count, you can use this code:
RowCount = Range("A100000").End(xlUp).Row
It means select "A100000" cell and go up to find a cell that not null. It shows the last row of our table on A column.
Sub CreateTableD()
Dim WB As Workbook
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim i As Long
Dim k As Long
'Dim n As Long
Set WB = Excel.ActiveWorkbook
Set WS1 = WB.Worksheets("List1")
Set WS2 = WB.Worksheets("List2")
i = 1
k = 1
'While Not IsEmpty(WS1.Cells(i, 1))
Do While WS1.Cells(i, 1).Value <> ""
If (WS1.Cells(i, 4).Value = "Depo" And WS1.Cells(i, 8).Value = "CZK") Then
WS2.Cells(k + 1, 4).Value = WS1.Cells(i, 7).Value
WS2.Cells(k + 2, 4).Value = WS1.Cells(i, 7).Value
WS2.Cells(k + 1, 7).Value = "79010000"
WS2.Cells(k + 2, 7).Value = "79010000"
ElseIf (WS1.Cells(i, 4).Value = "Loan" And WS1.Cells(i, 8).Value = "CZK") Then
WS2.Cells(k + 1, 4).Value = WS1.Cells(i, 7).Value
WS2.Cells(k + 2, 4).Value = WS1.Cells(i, 7).Value
WS2.Cells(k + 1, 7).Value = "75010000"
WS2.Cells(k + 2, 7).Value = "75010000"
k = k + 2
End If
i = i + 1
'Wend
Loop
Range("D1").Select
ActiveCell.FormulaR1C1 = "CZK"
End Sub
Hi. I have a code, but it doesnt work properly. If two conditions are satisfied it must return interest on another worksheet and also some static data( which is in the code) I've shown the right result on second picture.
first worksheet with conditions
on this picture i showed what i need to get
The problem is that you are only incrementing k when it is a loan.
If (WS1.Cells(i, 4).Value = "Depo" And WS1.Cells(i, 8).Value = "CZK") Then
ElseIf (WS1.Cells(i, 4).Value = "Loan" And WS1.Cells(i, 8).Value = "CZK") Then
k = k + 2
End If
Incrementing k when eith condition is True will fix the problem.
If (WS1.Cells(i, 4).Value = "Depo" And WS1.Cells(i, 8).Value = "CZK") Then
k = k + 2
ElseIf (WS1.Cells(i, 4).Value = "Loan" And WS1.Cells(i, 8).Value = "CZK") Then
k = k + 2
End If
I usually create a separate function to handle adding data to a table. Breaking up the code into smaller units helps simplify debugging.
Here is how I would write it.
Sub CreateTableD()
Dim x As Long
With Worksheets("List1")
For x = 2 To .Range("D" & .Rows.Count).End(xlUp).Row
If .Cells(x, 8).Value = "CZK" Then
If .Cells(x, 4).Value = "Depo" Then
AddList2Entry .Cells(x, 7).Value, "79010000"
AddList2Entry .Cells(x, 7).Value, "79010000"
ElseIf .Cells(x, 4).Value = "Loan" Then
AddList2Entry .Cells(x, 7).Value, "75010000"
AddList2Entry .Cells(x, 7).Value, "75010000"
End If
End If
Next
End With
End Sub
Sub AddList2Entry(interest As Double, StaticValue As Double)
Dim newRow As Long
With Worksheets("List2")
newRow = .Range("D" & .Rows.Count).End(xlUp).Row + 1
.Cells(newRow, "D").Value = interest
.Cells(newRow, "G").Value = StaticValue
End With
End Sub
When I run my code, I keep getting a type mismatch error even though all the variables are defined as variants. I'm not sure what the issue is. I'm kind of new to VBA so I would appreciate any help! Thanks!
Sub drink_2()
Columns("E:H").Insert shift:=xlToRight, copyorigin:=xlFormatFromLeftOrAbove
Range("F6").value = "Drink Price"
Range("G6").value = "Drink Revenue"
Range("H6").value = "Gross Sales less Drink Revenue"
Dim i As Variant
Dim item As Variant
Dim drink_price As Variant
Dim wbk As Workbook
Set wbk = Workbooks.Open("C:\Users\username\Documents\vlookup table drink prices.xlsx")
Dim lookup_range As Variant
lookup_range = wbk.Worksheets("Sheet1").Range("A:B").value
i = 7
Do While Cells(i, 1).value <> ""
item = Cells(i, 1).value
Cells(11, 1).value = item
drink_price = Application.WorksheetFunction.VLookup(item, lookup_range, 2, False)
zero_check = Application.WorksheetFunction.IfError(drink_price, 0)
If IsError(Application.WorksheetFunction.VLookup(item, lookup_range, 2, False)) Then
Cells(i, 6).value = ""
Else
Cells(i, 6).value = Application.WorksheetFunction.VLookup(item, lookup_range, 2, False)
End If
Cells(i, 7).Formula = Cells(i, 6).value * Cells(i, 5).value
Cells(i, 8).Formula = Cells(i, 4).value - Cells(i, 7).value
Range("F:G").NumberFormat = "#,##0.00"
i = i + 1
Loop
Cells.EntireColumn.AutoFit
End Sub
This is probably the problem line:
lookup_range = wbk.Worksheets("Sheet1").Range("A:B").value
You should be getting a range, not a value, so you need to use the set keyword and drop the value property.
set lookup_range = wbk.Worksheets("Sheet1").Range("A:B")
I have a data set as follows:
In essence I need a duplicate row (bar the project) to be deleted and for the project to be moved onto the first line and to the right of the other one.
Example of
I have had very little experience with VBA and any help on where to start would be much appreciated.
This should be straight-forward to follow, any questions just ask
Public Sub MergeProjects()
Dim lastrow As Long
Dim lastcol As Long
Dim i As Long
Application.ScreenUpdating = False
With ActiveSheet
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = lastrow - 1 To 2 Step -1
If .Cells(i + 1, "A").Value = .Cells(i, "A").Value And _
.Cells(i + 1, "B").Value = .Cells(i, "B").Value And _
.Cells(i + 1, "C").Value = .Cells(i, "C").Value And _
.Cells(i + 1, "D").Value = .Cells(i, "D").Value And _
.Cells(i + 1, "E").Value = .Cells(i, "E").Value Then
lastcol = .Cells(i, "A").End(xlToRight).Column
.Cells(i + 1, "F").Resize(, 100).Copy .Cells(i, lastcol + 1)
.Rows(i + 1).Delete
End If
Next i
lastcol = .Range("A1").CurrentRegion.Columns.Count
.Range("F1:G1").Value = Array("Project 1", "Project 2")
If lastcol > 7 Then
.Range("F1:G1").AutoFill .Range("F1").Resize(, lastcol - 5)
End If
End With
Application.ScreenUpdating = True
End Sub
I have a very large excel file, around 50K rows. In Column (C) I have the person numbers 1,1,1,1,2,3,4,5,.... and in Column(N) I have the payments for this person, so the data looks like
What I want is to sum All payments for one person from column N and set the result in column O, then merge column O.
It looks like you're looking for the built-in excel SUMIF function
=sumif(C:C,"specific person number",N:N)
Sub summAndMerge()
lastrow = Worksheets("A").Range("A65536").End(xlUp).Row
For i = 2 To lastrow
If Cells(i, 1).Value <> Cells(i - 1, 1).Value And Cells(i, 1).Value <> Cells(i + 1, 1).Value Then
intUpper = i
Debug.Print ("<> -1 and <> +1 " & intUpper)
End If
If Cells(i, 1).Value <> Cells(i - 1, 1).Value And Cells(i, 1).Value = Cells(i + 1, 1).Value Then
intUpper = i
Debug.Print ("<> -1 and = +1 " & intUpper & " UPPPER LIMIT")
End If
If Cells(i, 1).Value <> Cells(i + 1, 1).Value And Cells(i, 1).Value = Cells(i - 1, 1).Value Then
Application.DisplayAlerts = False
Debug.Print ("<> +1 and = -1:" & i & "LOWER LIMIT")
Range(Cells(intUpper, 3), Cells(i, 3)).Merge
Cells(intUpper, 3).Value = "=sumif(B" & CStr(intUpper) & ":B" & CStr(i) & ","">0"")"
Range(Cells(i, 1), Cells(i, 24)).Borders(xlEdgeBottom).LineStyle = xlDouble
End If
If Cells(i, 1).Value <> Cells(i + 1, 1).Value And Cells(i, 1).Value <> Cells(i - 1, 1).Value Then
Debug.Print ("One Cells: " & i)
Range(Cells(i, 1), Cells(i, 24)).Borders(xlEdgeBottom).LineStyle = xlDouble
Cells(intUpper, 3).Value = Cells(intUpper, 2).Value
End If
Next i
End Sub