VBA - Adding a name inside the IF - vba

I'm having some issue regarding the below code:
Sub Sendorders()
Application.ScreenUpdating = False
Dim wb As Workbook: Set wb = Workbooks("Hugo Automate V15.xlsm")
Dim ws As Worksheet: Set ws = wb.Worksheets("Bid-Ask")
Dim i As Long
ws.Activate
ws.Cells(2, 20) = ws.Cells(2, 20) & " Equity"
For i = 8 To 242
If ws.Cells(2, 20).Value = ws.Cells(i, 1).Value Then
If Cells(2, 23) = "BUY" Then
Cells(i, 20) = Cells(2, 22)
Cells(i, 21) = Cells(2, 21)
Else
Cells(i, 22) = Cells(2, 22)
Cells(i, 23) = Cells(2, 21)
End If
End If
Next
Application.ScreenUpdating = True
End Sub
I would like to compile the ligne 7 with the ligne 10 like this:
If ws.Cells(2,20) & "Equity".Value = ws.Cells(i,1).Value Then
However this doesn't work...

.value is a property of a Range object. It is also the default property used when looking at a range object.
As such, ws.Cells(2,20) from line 7 and ws.Cells(2, 20).Value from the if is the same thing.
Line 7 is therefore also this: ws.Cells(2, 20).value = ws.Cells(2, 20).value & " Equity"
The problem is trying to use the .value property, but not connected to the range object, but to the string: ws.Cells(2,20) & "Equity".Value, this will not work.
You can also you "With" instead of Activate to make sure you are targeting the right sheet, unless you specifically want to switch to that sheet.
With ws
For i = 8 To 242
If .Cells(2, 20).Value & " Equity" = .Cells(i, 1).Value Then
If .Cells(2, 23) = "BUY" Then
.Cells(i, 20) = .Cells(2, 22)
.Cells(i, 21) = .Cells(2, 21)
Else
.Cells(i, 22) = .Cells(2, 22)
.Cells(i, 23) = .Cells(2, 21)
End If
End If
Next i
End with

Related

looping throught worhsheet excel vba and return data on another worksheet

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

I keep getting type mismatch when i run my code

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")

Replace range of data if target value already exists

The following script selects a range of data on one sheet and transfers the selection to another sheet.
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
Range(Cells(i, 1), Cells(i, 4)).Select
Selection.Copy
erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
If Cells(i, 1) <> "" Then Cells(i, 22).Value = "Yes"
If Cells(i, 22) <> "" Then Cells(i, 23).Value = Now
If Cells(i, 23) <> "" Then Cells(i, 24).Value = Environ("UserName")
ActiveWorkbook.Save
End If
Next i
I would now like to introduce a script which will replace the row of data on the target sheet if the value in column A already exists, but i'm not sure how to achieve this, any help is much appreciated.
Thank you in advance.
Public Function IsIn(li, Val) As Boolean
IsIn = False
Dim c
For Each c In li
If c = Val Then
IsIn = True
Exit Function
End If
Next c
End Function
dim a: a= range(destWB.sheet(whatever)..range("A1"),destWB.Range("A" & destWB.sheet(whatever).Rows.Count).End(xlUp)).value
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
if isin(a, Cells(i, 1) ) then
do whatever you want
else
If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
Range(Cells(i, 1), Cells(i, 4)).Select
Selection.Copy
erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
If Cells(i, 1) <> "" Then Cells(i, 22).Value = "Yes"
If Cells(i, 22) <> "" Then Cells(i, 23).Value = Now
If Cells(i, 23) <> "" Then Cells(i, 24).Value = Environ("UserName")
ActiveWorkbook.save
End If
End If
Next i
I suggest using a Dictionary-Object which is most likely a Hash-Map. The advantage is that you can use the built in method Dictionary.Exists(Key) to check if the Dictionary already holds the specified value (Key).
Also you should not save the Workbook in every step of the iteration. It would be better (and faster) to only save the workbook after completing the copying of your whole data.
Additionally your If-Tests after copy-paste are not neccessary, because you are already checking for Cells(i,1)<>"" before copying so you don't have to check this again as it does not change.
The following code shows how to get your desired result:
Set dict = CreateObject("Scripting.Dictionary")
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To LastRow
If Cells(i, 1) <> "" And Cells(i, 21) = "OK" And Cells(i, 22) <> "Yes" Then
If dict.Exists(Cells(i,1).Value) Then
'value already exists -> update row number
dict.Item(Cells(i,1).Value)=i
Else
'save value of column A and row number in dictionary
dict.Add Cells(i,1).Value, i
End If
Cells(i, 22).Value = "Yes"
Cells(i, 23).Value = Now
Cells(i, 24).Value = Environ("UserName")
End If
Next i
'finally copy over your data (only unique values)
For Each i In dict.Items
Range(Cells(i, 1), Cells(i, 4)).Select
Selection.Copy
erow = Worksheets("iForms").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
Worksheets("iForms").Cells(erow, 1).PasteSpecial Paste:=xlPasteValues
Next i

Have search function need help editing

So I have a code I have written the first part of the code is to create a new worksheet with the headings specified. The second part of the code is meant to populate that table with certain information. The problem I am having is getting the correct bits of information to go into the correct columns.
I need the code to search for the value 9.1 in column G in all worksheets within a workbook
if that value is found I need it to copy this to column b in the new sheet along with the following information :
Engine Effect from Column F Same row must be pasted to Column C in the worksheet entitled FHA
Part number is always located in Cell J3 this must be pasted into column D and is always the same
Part Name Is Always located in C2 this must be pasted into column E and is always the same
FM ID from Column B same row must be pasted to Column F in the worksheet entitled FHA
Failure Mode & Cause from Column C Same row must be pasted to column G in FHA
FMCN Value From Column N pasted to Column H In FHA
As It stands the code I have is
Sub createWSheetFHA()
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "FHA"
Cells(1, 2) = "FHA TABLE"
Cells(2, 2) = "FHA Ref"
Cells(2, 3) = "Engine Effect"
Cells(2, 4) = "Part No"
Cells(2, 5) = "Part Name"
Cells(2, 6) = "FM I.D"
Cells(2, 7) = "Failure Mode & Cause"
Cells(2, 8) = "FMCM"
Cells(2, 9) = "PTR"
Cells(2, 10) = "ETR"
Range(Cells(2, 2), Cells(2, 10)).Font.Bold = True
Range(Cells(1, 2), Cells(1, 10)).MergeCells = True
Range(Cells(1, 2), Cells(1, 10)).Font.Bold = True
End Sub
Sub Populate_FHA_Table_2()
Dim wks As Excel.Worksheet, i As Integer, n As Integer
Application.ScreenUpdating = False
Sheets("FHA").Range("A2:" & Columns.Count & ":" & Rows.Count).Delete
i = 1
For Each wks In ActiveWorkbook.Worksheets
If wks.Name <> "FHA" Then
wks.UsedRange.AutoFilter Field:=7, Criteria1:="9.1"
Sheets(i).Range(Sheets(i).Range("G1").Offset(1), Sheets(i).Range("B1").End(xlDown)).Copy _
Sheets("FHA").Range("C" & Rows.Count).End(xlUp)
Sheets(i).Range(Sheets(i).Range("F1").Offset(1), Sheets(i).Range("D1").End(xlDown)).Copy _
Sheets("FHA").Range("d" & Rows.Count).End(xlUp)
Sheets(i).Range(Sheets(i).Range("J1").Offset(1), Sheets(i).Range("E1").End(xlDown)).Copy _
Sheets("FHA").Range("e" & Rows.Count).End(xlUp)
Sheets(i).Range(Sheets(i).Range("C1").Offset(1), Sheets(i).Range("H1").End(xlDown)).Copy _
Sheets("FHA").Range("E" & Rows.Count).End(xlUp)
Sheets(i).Range(Sheets(i).Range("B1").Offset(1), Sheets(i).Range("H1").End(xlDown)).Copy _
Sheets("FHA").Range("F" & Rows.Count).End(xlUp)
Sheets(i).Range(Sheets(i).Range("C1").Offset(1), Sheets(i).Range("H1").End(xlDown)).Copy _
Sheets("FHA").Range("G" & Rows.Count).End(xlUp)
Sheets(i).Range(Sheets(i).Range("N1").Offset(1), Sheets(i).Range("H1").End(xlDown)).Copy _
Sheets("FHA").Range("H" & Rows.Count).End(xlUp)
wks.UsedRange.AutoFilter
End If
i = i + 1
Next
Application.ScreenUpdating = True
End Sub
You have some mismatches in your code (Example using 'for each wk' then accessing via an index 'i'; where they may not necessarily match)
Try something like this...
I have added in some dynamic flow control which isn't strictly needed but if and when your headers change in the future, it may be easier to have it in this form.
Likewise I have tried to add in some error handling as well
Sub Create_FHA_Sheet()
Dim Headers() As String: Headers = _
Split("FHA Ref,Engine Effect,Part No,Part Name,FM I.D,Failure Mode & Cause,FMCM,PTR,ETR", ",")
If Not WorksheetExists("FHA") Then Worksheets.Add().Name = "FHA"
Dim wsFHA As Worksheet: Set wsFHA = Sheets("FHA")
wsFHA.Move after:=Worksheets(Worksheets.Count)
wsFHA.Cells.Clear
Application.ScreenUpdating = False
With wsFHA
For i = 0 To UBound(Headers)
.Cells(2, i + 2) = Headers(i)
.Columns(i + 2).EntireColumn.AutoFit
Next i
.Cells(1, 2) = "FHA TABLE"
.Range(.Cells(1, 2), .Cells(1, UBound(Headers) + 2)).MergeCells = True
.Range(.Cells(1, 2), .Cells(1, UBound(Headers) + 2)).HorizontalAlignment = xlCenter
.Range(.Cells(1, 2), .Cells(2, UBound(Headers) + 2)).Font.Bold = True
End With
Dim RowCounter As Long: RowCounter = 3
Dim SearchTarget As String: SearchTarget = "9.1"
Dim SourceCell As Range, FirstAdr As String
If Worksheets.Count > 1 Then
For i = 1 To Worksheets.Count - 1
With Sheets(i)
Set SourceCell = .Columns(7).Find(SearchTarget, LookAt:=xlWhole)
If Not SourceCell Is Nothing Then
FirstAdr = SourceCell.Address
Do
wsFHA.Cells(RowCounter, 3).Value = .Cells(SourceCell.Row, 6).Value
wsFHA.Cells(RowCounter, 4).Value = .Cells(3, 10).Value
wsFHA.Cells(RowCounter, 5).Value = .Cells(2, 3).Value
wsFHA.Cells(RowCounter, 6).Value = .Cells(SourceCell.Row, 2).Value
wsFHA.Cells(RowCounter, 7).Value = .Cells(SourceCell.Row, 3).Value
wsFHA.Cells(RowCounter, 8).Value = .Cells(SourceCell.Row, 14).Value
Set SourceCell = .Columns(7).FindNext(SourceCell)
RowCounter = RowCounter + 1
Loop While Not SourceCell Is Nothing And SourceCell.Address <> FirstAdr
End If
End With
Next i
End If
Application.ScreenUpdating = True
End Sub
Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean
On Error Resume Next
WorksheetExists = (ThisWorkbook.Sheets(WorksheetName).Name <> "")
On Error GoTo 0
End Function

To copy data from 232 excel worksheets present in a single excel workbook into a destination worksheet.The row name and worksheet name should be same

To copy data from 232 excel worksheets present in a single excel workbook into a destination excel worksheet present in the same workbook with the criteria that row name in the destination worksheet should be same as worksheetname.Since while naming a worksheet in an excel file we have restriction in size,so for some worksheet the names are written short but in the destiantion worksheet the names are written in fullform.So for some worksheets the data is not accumulated.How to fix this issue?Please help.I am new to VBA
Private Sub CommandButton1_Click()
Dim sheetName As String
Dim i As Integer
i = 2
Do While Cells(i, 1).Value <> ""
sheetName = Cells(i, 1)
Cells(i, 4) = Sheets(sheetName).Cells(190, 7).Value
Cells(i, 5) = Sheets(sheetName).Cells(191, 7).Value
Cells(i, 6) = Sheets(sheetName).Cells(192, 7).Value
Cells(i, 7) = Sheets(sheetName).Cells(193, 7).Value
Cells(i, 8) = Sheets(sheetName).Cells(194, 7).Value
Cells(i, 9) = Sheets(sheetName).Cells(195, 7).Value
Cells(i, 10) = Sheets(sheetName).Cells(196, 7).Value
i = i + 1
Loop
End Sub
Try to use following code (since max length for sheet name when renaming a worksheet manually is 31 character you can restrict sheetName):
Private Sub CommandButton1_Click()
Dim sheetName As String
Dim i As Integer
i = 2
Do While Cells(i, 1).Value <> ""
sheetName = IIf(Len(Cells(i, 1))>31,Left(Cells(i, 1),31),Cells(i, 1))
Cells(i, 4) = Sheets(sheetName).Cells(190, 7).Value
Cells(i, 5) = Sheets(sheetName).Cells(191, 7).Value
Cells(i, 6) = Sheets(sheetName).Cells(192, 7).Value
Cells(i, 7) = Sheets(sheetName).Cells(193, 7).Value
Cells(i, 8) = Sheets(sheetName).Cells(194, 7).Value
Cells(i, 9) = Sheets(sheetName).Cells(195, 7).Value
Cells(i, 10) = Sheets(sheetName).Cells(196, 7).Value
i = i + 1
Loop
End Sub