I am trying to make a VBA scrip that check all cells between B2 and B60 for the text "Ja" that's yes in Norwegian.
How can I make this a little bit simpler that making a "if" command for each cell?
I want it to, if the cell contains "ja"(yes) then write to colum D and the same number.
ie. B1,2,3,4,5 cotains "ja", I need it to take the previous cell value in D1.2,3,4,5 and add another digit to it +1.
If nothing is found in B(ie.false) it needs to write "NEI" in the current cell, and if "NEI" (no) is found in that cell it adds +1 to colum E
Sub Macro2()
Dim celltxt As String
Dim a As Variant
If IsEmpty(Range("B2").Value) = True Then
Cells(2, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B2").Text
If InStr(1, celltxt, "ja") Then
a = Cells(2, 1).Value
'write to cell
Cells(2, 4).Value = Cells(2, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(2, 5).Value = Cells(2, 5) + 1
End If
If IsEmpty(Range("B3").Value) = True Then
Cells(3, 2).Value = "NEI"
End If
celltxt = ActiveSheet.Range("B3").Text
If InStr(1, celltxt, "ja") Then
a = Cells(3, 1).Value
'write to cell
Cells(3, 4).Value = Cells(3, 4) + 1
Else
'antall Cw'er vedkommende IKKE har deltatt på
Cells(3, 5).Value = Cells(3, 5) + 1
End If
End Sub
Sub slettingALL()
Range("D2:E55").Select
Selection.ClearContents
End Sub
Sub slettingdeltakelse()
Range("B2:B60").Select
Selection.ClearContents
End Sub
The following code uses a For Each loop and an IF THEN ELSE statement to check for the value "JA" in the range B2:B60.
If it finds "JA", it looks two columns to the right from the current i location, and adds "+1" to the value above it. If it finds nothing, it writes "NEI" to the current i location, and then moves three columns to the right and adds +1 to the value above it.
Sub Macro2()
For Each i In Range(Cells(2, 2), Cells(60, 2))
If i.Value = "JA" Then
i.Offset(0, 2).Value = i.Offset(-1, 2).Value + 1
Else
i.Value = "NEI"
i.Offset(0, 3).Value = i.Offset(-1, 3).Value + 1
End If
Next i
End Sub
Please let me know if this code does not work for your purpose.
Related
Sub a()
Dim i As Integer
For i = 1 To 8
If Cells(i, 1).Value = "M" Then
Cells(i, 2).Value = ""
End If
Next i
End Sub
You need to include the sheet information along with the cells.
Example: Sheet1.Cells(i,1).Value or Worksheets("Sheet name").Cells(i,1).Value
My VBA script errors when I try to insert data from Sheet1 into Sheet2. Script code delivers only "Case 2-3" ROW numbers, first "Case" does not input into Sheet 2. Wondering what else should be included in VBA script to finalize processes?
My VBA Script:
Sub CopyFromSheet1()
Dim i As Long
For i = 1 To Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F
Select Case CStr(Sheet1.Cells(i, 3).Value) ' Looks at the Value in Column C
Case "Due From"
Sheet2.Cells(22, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL1"
Sheet2.Cells(23, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL2"
Sheet2.Cells(24, 3).Value = Sheet1.Cells(i, 6).Value
End Select
Next i
End Sub
What about this: Do you everything your doing, but just for Column C. And then Do it all again for column D?
Sub CopyFromSheet1()
Dim i As Long
Dim col as Long
for col = 3 to 4
For i = 1 To Sheet1.Cells(Sheet1.Rows.Count, 6).End(xlUp).Row ' Last Cell of Column F
Select Case CStr(Sheet1.Cells(i, col).Value)
Case "Due From"
Sheet2.Cells(22, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL1"
Sheet2.Cells(23, 3).Value = Sheet1.Cells(i, 6).Value
Case "TOTAL2"
Sheet2.Cells(24, 3).Value = Sheet1.Cells(i, 6).Value
End Select
Next i
next col
End Sub
I have a sheet where the number of rows is dynamic. I am trying to add a macro which adds a new row after each active row and it should add the text "No Show" under column C of each new row added and the it should add the cell value E5 under D column.
Here is the example below:
Current Sheet:
After the Macro: (Test in E5 is Holiday)
I have a macro to add new empty rows but not sure how to integrate the other pieces of it.
Sub Insert_Blank_Rows()
Selection.End(xlDown).Select
Do Until ActiveCell.Row = 1
ActiveCell.EntireRow.Insert shift:=xlDown
ActiveCell.Offset(-1, 0).Select
Loop
End Sub
Sub FFF()
Dim r&, vE5
vE5 = [E5]: r = Cells(Rows.Count, 1).End(xlUp).Row + 1
While r > 1
Rows(r).Insert
Cells(r, 1).Resize(, 4) = Array(Cells(r - 1, 1).Resize(, 2), "No Show", vE5)
r = r - 1
Wend
End Sub
loop backwards:
Option Explicit
Sub Insert_Blank_Rows()
Dim iRow As Long
Dim myText As String
myText = Range("E5").Text
With Selection
For iRow = .Rows.Count To 1 Step -1
.Rows(iRow + 1).EntireRow.Insert shift:=xlDown
With .Rows(iRow + 1)
.Range("A1:B1").Value = .Offset(-1).Range("A1:B1").Value
.Range("C1:D1").Value = Array("No Show", myText)
End With
Next
End With
End Sub
if i understood your question you can:
in this example i suppose that you have in cell E5 the text Holiday.
i tried at no change your code
EDITED THE IMAGE and CODE
(because before i used E1 cell and i don't write AB... into new column)
BEFORE EXECUTE THE MACRO
AFTER MACRO
Sub Insert_Blank_Rows()
Dim text, textCell_E5 As String
Dim myRow As Long
text = "no Show" ' this thext goes into column C
textCell_E5 = Cells(5, 5) ' Holiday
ActiveSheet.Range("A1").Select ' or cells(1,1).Activate
Selection.End(xlDown).Select
myRow = ActiveCell.Row + 1
Cells(myRow, 1).Offset(0, 2) = text
Cells(myRow, 1).Offset(0, 3) = textCell_E5
Cells(myRow, 1).Offset(0, 0) = Cells(myRow, 1).Offset(-1, 0)
Cells(myRow, 1).Offset(0, 1) = Cells(myRow, 1).Offset(-1, 1)
Do Until ActiveCell.Row = 1
ActiveCell.EntireRow.Insert shift:=xlDown
myRow = ActiveCell.Row ' get the current row
Cells(myRow, 1).Offset(0, 2) = text ' write into column C the no Show
Cells(myRow, 1).Offset(0, 3) = textCell_E5 ' add Holiday Text
Cells(myRow, 1).Offset(0, 0) = Cells(myRow, 1).Offset(-1, 0) 'write into column A (new row)
Cells(myRow, 1).Offset(0, 1) = Cells(myRow, 1).Offset(-1, 1) ' write into column B (new row)
ActiveCell.Offset(-1, 0).Select
Loop
End Sub
I Tried the code and works.
Hope this helps
I need help fixing my code and adding in the cell ranges.
I am trying to change the values in the cells to being the correct values if they are spelt incorrectly. But the table will be added to so I need to make it a flexible code. The code currently stops at the beginning sub with error code 424. I am fairly new to VBA and am stuck.
Sub Consolidates()
Dim datasheet As Worksheet
Set datasheet = ThisWorkbook.Sheets("sheet1")
lr = datasheet.Cells(Rows.Count, 9).End(xlUp).Row
For x = 2 To lr
If cell.Value = "B" Or "BR" Or " Then
cell.Value = "BR"
ElseIf cell.Value = "CL" Or "CR" _
Then cell.Value = "CR"
ElseIf cell.Value = "" Then
End If
Next x
End Sub
you could use something like follows
Option Explicit
Sub Consolidates()
Dim stringsToSearch As String, stringToSubstitute As String
Dim stringsToSearchArr As Variant, stringToSubstituteArr As Variant
' here define the "table"
stringsToSearch = "B,CL" '<--| type here the strings to be searched for
stringToSubstitute = "BR,CR" '<--| type here the corresponding strings to change searched ones into
stringsToSearchArr = Split(stringsToSearch, ",") '<--| turn "stringsToSearch" into an array
stringToSubstituteArr = Split(stringToSubstitute, ",") '<--| turn "stringToSubstitute" into an array
With ThisWorkbook.Sheets("sheetTest") '<--| change "sheetTest" with your actual sheet name
With .Range("I2:I" & .Cells(.Rows.Count, 9).End(xlUp).Row) '<--| consider all cells in column "I" from row 2 to last non empty one
For i = LBound(stringsToSearchArr) To UBound(stringsToSearchArr) '<--| loop through the "table"
.Replace What:=stringsToSearchArr(i), Replacement:=stringToSubstituteArr(i), LookAt:=xlWhole, MatchCase:=True '<--| find current string and replace it with its corresponding replacement
Next i
End With
End With
End Sub
Cell needs a reference to which cell. Also you can't use the or statement like that. Below a simple way to get it done.
For x = 1 To lr
If Cells(x, 9).Value = "B" Or Cells(x, 9).Value = "BR" Then
Cells(x, 9).Value = "BR"
ElseIf Cells(x, 9).Value = "CL" Or Cells(x, 9).Value = "CR" Then
Cells(x, 9).Value = "CR"
End If
Next x
You should consider a select statement
For x = 1 To lr
Select Case Cells(x, 9).Value
Case "B", "BR"
Cells(x, 9).Value = "BR"
Case "CL", "CR"
Cells(x, 9).Value = "CR"
End Select
Next x
Since it is case sensitive you could add an Lcase which could save you some time
For x = 1 To lr
Select Case LCase(Cells(x, 9).Value)
Case "b", "br"
Cells(x, 9).Value = "BR"
Case "cl", "cr"
Cells(x, 9).Value = "CR"
End Select
Next x
I am trying to write a code that will take one cell and then iterate through another column to find a match, once it has found a match it will then match two other cells in that same row and return the value of a 5th and 6th cell. However, it is not working! any suggestions??
Sub rates()
Dim i As Integer
For i = 2 To 2187
If Cells(i, 1).Value = Cells(i, 11).Value Then
If Cells(i, 2).Value = Cells(i, 12).Value Then
Cells(i, 20) = Cells(i, 1).Value
Cells(i, 21) = Cells(i, 11).Value
Cells(i, 22) = Cells(i, 4).Value
Cells(i, 23) = Cells(i, 16).Value
Else
Cells(i, 24) = "No match"
End If
End If
Next i
End Sub
Try fully qualifying your cell objects i.e. sheet1.cells(i,1).value etc or encase within a with statement i.e.
with sheet1
if .cells(i,X) = .cells(i,Y) then
'...etc
end with
I think the default property for a range is "Value" but try putting .Value on to the end of all those Cell lines too... like you have for half of them :)
[EDIT/Addition:]
... failing that, you're not actually searching a whole column at any point: try something like:
Sub rates()
Dim i As Integer
Dim rgSearch As Range
Dim rgMatch As Range
Dim stAddress As String
Dim blMatch As Boolean
With wsSheet
Set rgSearch = .Range(.Cells(x1, y1), .Cells(x2, y2)) ' Replace where appropriate (y = 1 or 11 i guess, x = start and end row)
End With
For i = 2 To 2187
Set rgMatch = rgSearch.Find(wsSheet.Cells(i, y)) ' y = 1 or 11 (opposite of above!)
blMatch = False
If Not rgMatch Is Nothing Then
stAddress = rgMatch.Address
Do Until rgMatch Is Nothing Or rgMatch.Address = stAddress
If rgMatch.Offset(0, y).Value = Cells(i, 12).Value Then
Cells(i, 20) = Cells(i, 1).Value
Cells(i, 21) = Cells(i, 11).Value
Cells(i, 22) = Cells(i, 4).Value
Cells(i, 23) = Cells(i, 16).Value
blMatch = True
Else
End If
Set rgMatch = rgSearch.FindNext(rgMatch)
Loop
End If
If Not blMatch Then
Cells(i, 24) = "No match"
End If
Next i
End Sub
I've made a lot of assumptions in there and there's a few variables you'll have to replace. You could also probably use application.worksheetfunction.match but .find is quicker and more awesome