I need help in Excel. My question is: How can I get the first row of each row in this loop and print the output.
Input Column and Row value is like this:
col1 col2 col3
1 test abc
2 tests dfg
3 gtd gdd
Output like this.
(col1,col2,col3)('1','test','abc');
(col1,col2,col3)('2','tests','dfg');
(col1,col2,col3)('3','gtd','gdd');
The Code that I am working on is
For i = 1 To LastRow
For j = 3 To LastCol
If IsNumeric(Cells(i, j)) & Cells(i, j) > 0 = True Then
vaString = vaString & Cells(i, j)
End If
If j <> LastCol Then vaString = vaString & ","
If j = LastCol Then vaString = vaString
Next j
myString = myString
Next i
Thanks in advance
From your parameters I will assume your data starts from cell C1. Change the first few lines if otherwise.
Sub Testing()
Dim FirstRow As Integer, FirstCol As Integer, LastRow As Integer, LastCol As Integer
FirstRow = 1
FirstCol = 3
LastRow = 4
LastCol = 5
Dim arrStr() As String
Dim strFirstRow As String
Dim strPath As String
strPath = "C:\..." ' Path of your choice
Open strPath For Append As #1
ReDim arrStr(FirstCol To LastCol)
For j = FirstCol To LastCol
arrStr(j) = CStr(Cells(FirstRow, j))
Next j
strFirstRow = "(" & Join(arrStr, ",") & ")"
For i = FirstRow + 1 To LastRow
If IsNumeric(Cells(i, FirstCol).Value) Then
If Cells(i, FirstCol).Value > 0 Then
ReDim arrStr(FirstCol To LastCol)
For j = FirstCol To LastCol
arrStr(j) = "'" & CStr(Cells(i, j)) & "'"
Next j
Debug.Print strFirstRow & "(" & Join(arrStr, ",") & ");"
Print #1, strFirstRow & "(" & Join(arrStr, ",") & ");"
End If
End If
Next i
Close #1
End Sub
Here is a rather simple example making use of array variables:
Sub Test()
Dim x As Long
Dim str1 As String, str2 As String
Dim arr1() As Variant, arr2() As Variant
With ThisWorkbook.Sheets("Sheet1") 'Change according to your sheetname
arr1 = Application.Transpose(Application.Transpose(.Range("A1:C1").Value2))
arr2 = .Range("A2:C" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value2
str1 = "(" & Join(arr1, ",") & ")"
For x = LBound(arr2) To UBound(arr2)
str2 = "(" & Join(Array(arr2(x, 1), arr2(x, 2), arr2(x, 3)), ",") & ")" & ";"
Debug.Print str1 & str2 'Do something with the full string
Next x
End With
End Sub
Related
I have a data like this :
A049
A050
A051
A053
A054
A055
A056
A062
A064
A065
A066
And I want the output like :
As you can see, I want the ranges which are in consecutive order
I am trying some thing like this:
Private Sub CommandButton1_Click()
Set wb = ThisWorkbook
lastRow = wb.Sheets("Sheet1").Range("A" & wb.Sheets("Sheet1").Rows.Count).End(xlUp).Row
For i = 2 To lastRow
r = wb.Sheets("Sheet1").Range("A" & i).Value
If wb.Sheets("Sheet1").Range("A" & i).Value = wb.Sheets("Sheet1").Range("A" & i+1).Value
Next i
End Sub
But not helping me
Am feeling charitable so have tried some code which should work. It assumes your starting values are in A1 down and puts results in C1 down.
Sub x()
Dim v1, v2(), i As Long, j As Long
v1 = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
ReDim v2(1 To UBound(v1, 1), 1 To 2)
For i = LBound(v1, 1) To UBound(v1, 1)
j = j + 1
v2(j, 1) = v1(i, 1)
If i <> UBound(v1, 1) Then
Do While Val(Right(v1(i + 1, 1), 3)) = Val(Right(v1(i, 1), 3)) + 1
i = i + 1
If i = UBound(v1, 1) Then
v2(j, 2) = v1(i, 1)
Exit Do
End If
Loop
End If
If v1(i, 1) <> v2(j, 1) Then v2(j, 2) = v1(i, 1)
Next i
Range("C1").Resize(j, 2) = v2
End Sub
Try the below code
Private Sub CommandButton1_Click()
Set wb = ThisWorkbook
lastRow = wb.Sheets("Sheet1").Range("A" & wb.Sheets("Sheet1").Rows.Count).End(xlUp).Row
Dim lastNum, Binsert As Integer
Dim firstCell, lastCell, currentCell As String
Binsert = 1
lastNum = getNum(wb.Sheets("Sheet1").Range("A1").Value)
firstCell = wb.Sheets("Sheet1").Range("A1").Value
For i = 2 To lastRow
activeNum = getNum(wb.Sheets("Sheet1").Range("A" & i).Value)
currentCell = wb.Sheets("Sheet1").Range("A" & i).Value
If (activeNum - lastNum) = 1 Then
'nothing
Else
lastCell = wb.Sheets("Sheet1").Range("A" & (i - 1)).Value
wb.Sheets("Sheet1").Range("B" & Binsert).FormulaR1C1() = firstCell
If (firstCell <> lastCell) Then
wb.Sheets("Sheet1").Range("C" & Binsert).FormulaR1C1() = lastCell
End If
Binsert = Binsert + 1
firstCell = wb.Sheets("Sheet1").Range("A" & i).Value
End If
lastNum = activeNum
Next i
'last entry
wb.Sheets("Sheet1").Range("B" & Binsert).FormulaR1C1() = firstCell
If (firstCell <> currentCell) Then
wb.Sheets("Sheet1").Range("C" & Binsert).FormulaR1C1() = currentCell
End If
End Sub
Public Function getNum(ByVal num As String) As Integer
getNum = Val(Mid(num, 2))
End Function
Another solution. It loops backwards from last row to first row.
Option Explicit
Public Sub FindConsecutiveValues()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim lRow As Long 'find last row
lRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Dim lVal As String 'remember last value (stop value)
lVal = ws.Range("A" & lRow).Value
Const fRow As Long = 2 'define first data row
Dim i As Long
For i = lRow To fRow Step -1 'loop from last row to first row backwards
Dim iVal As Long
iVal = Val(Right(ws.Range("A" & i).Value, Len(ws.Range("A" & i).Value) - 1)) 'get value of row i without A so we can calculate
Dim bVal As Long
bVal = 0 'reset value
If i <> fRow Then 'if we are on the first row there is no value before
bVal = Val(Right(ws.Range("A" & i - 1).Value, Len(ws.Range("A" & i - 1).Value) - 1)) 'get value of row i-1 without A
End If
If iVal - 1 = bVal Then
ws.Rows(i).Delete 'delete current row
Else
If lVal <> ws.Range("A" & i).Value Then 'if start and stop value are not the same …
ws.Range("B" & i).Value = lVal 'write stop value in column B
End If
lVal = ws.Range("A" & i - 1).Value 'remember now stop value
End If
Next i
End Sub
In Excel, how I can merge values of Column B based on common values on Column A?
Basically what I need is some thing like this
You can use this UDF:
Function TEXTJOINIFS(rng As Range, delim As String, ParamArray arr() As Variant)
Dim rngarr As Variant
rngarr = Intersect(rng, rng.Parent.UsedRange).Value
Dim condArr() As Boolean
ReDim condArr(1 To Intersect(rng, rng.Parent.UsedRange).Rows.Count) As Boolean
Dim i As Long
For i = LBound(arr) To UBound(arr) Step 2
Dim colArr() As Variant
colArr = Intersect(arr(i), arr(i).Parent.UsedRange).Value
Dim j As Long
For j = LBound(colArr, 1) To UBound(colArr, 1)
If Not condArr(j) Then
Dim charind As Long
charind = Application.Max(InStr(arr(i + 1), ">"), InStr(arr(i + 1), "<"), InStr(arr(i + 1), "="))
Dim opprnd As String
If charind = 0 Then
opprnd = "="
Else
opprnd = Left(arr(i + 1), charind)
End If
Dim t As String
t = """" & colArr(j, 1) & """" & opprnd & """" & Mid(arr(i + 1), charind + 1) & """"
If Not Application.Evaluate(t) Then condArr(j) = True
End If
Next j
Next i
For i = LBound(rngarr, 1) To UBound(rngarr, 1)
If Not condArr(i) Then
TEXTJOINIFS = TEXTJOINIFS & rngarr(i, 1) & delim
End If
Next i
TEXTJOINIFS = Left(TEXTJOINIFS, Len(TEXTJOINIFS) - Len(delim))
End Function
You would call it like this:
=IF(MATCH(A1,A:A,0)=ROW(A1),TEXTJOINIFS(B:B,", ",A:A,A1),"")
Now it does not matter if the data is sorted or not it will only put the output in column C where the value in Column A first appears.
Use an array formula:
=TEXTJOIN(", ",TRUE,IF(A$1:A$15=A1,B$1:B$15,""))
(Use CTRL-SHIFT-ENTER instead of ENTER to enter the formula)
Here is my untested code of course.
The code below uses 2 loops to add the information.
dim X as integer
dim X2 as integer
dim match as string
X = 1
do while sheets("sheet1").range("A" & X).value <> ""
sheets("sheet1").range("C" & X).value = sheets("sheet1").range("B" & X).value
match = sheets("sheet1").range("A" & X).value
X2 = X + 1
do while sheets("sheet1").range("A" & X2).value = match
sheets("sheet1").range("C" & X).value = sheets("sheet1").range("C" & X).value + ", " + sheets("sheet1").range("B" & X2).value
X2 = X2 + 1
loop
X = X2
X = X + 1
Loop
I'm trying to concatenate two ranges in excel using VBA so each cell in range1 concatenates all cells in range2 until cell A is null. Please see below:
Range1(column A): Range2(column B):
50703, 50702 52797, 52848
Concatenate(column C):
50703-52797, 50703-52848, 50702-52797, 50702-52848
This will insert all the combinations of values in column A and B into column C and concatenate them with a hyphen:
Sub combinations()
Dim i As Long, j As Long, n As Long
Dim valsColA As Variant, valsColB As Variant
With ThisWorkbook.Sheets("Combinations") ' change sheet name, if necessary
valsColA = .Range(.Cells(1, 1), .Range("A1").End(xlDown)).Value
valsColB = .Range(.Cells(1, 2), .Range("B1").End(xlDown)).Value
For i = LBound(valsColA) To UBound(valsColA)
For j = LBound(valsColB) To UBound(valsColB)
n = n + 1
.Cells(n, 3).Value = valsColA(i, 1) & "-" & valsColB(j, 1)
Next j
Next i
End With
End Sub
Here is what I came up with, although #Miqi180 got there first:
Sub ABPerm()
Dim Acol As Integer
Dim Bcol As Integer
Dim RowNumA As Integer
Dim RowNumB As Integer
Dim RowNumC As Integer
Acol = Range("A" & Rows.Count).End(xlUp).Row
Bcol = Range("B" & Rows.Count).End(xlUp).Row
RowNumA = 1
RowNumB = 1
RowNumC = 1
For a = 1 To Acol
For b = 1 To Bcol
Range("C" & RowNumC).Value = Range("A" & RowNumA).Value & "-" & Range("B" & RowNumB).Value
RowNumB = RowNumB + 1
RowNumC = RowNumC + 1
Next b
RowNumB = 1
RowNumA = RowNumA + 1
Next a
End Sub
Try this code
Sub Test()
Dim rng As Range
Set rng = Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
rng.Offset(, 2).Value = Evaluate("If(Row(1:" & rng.Rows.Count & ")," & rng.Address(, , , True) & " & " & "-" & rng.Offset(, 1).Address(, , , True) & ")")
End Sub
I am using following code to bring data from 1st sheet to 2nd using vlookup. The problem is 2nd sheet entries for which data is not present in 1st sheet are getting populated with previous entries of 2nd sheet. Can someone please help me understand my mistake?
Dim NIMsLastRow As Integer
Dim NIMsLastCol As Integer
Dim tempInt As Integer
Dim temp3 As String
Dim tempin As String
Dim ColLtr As String
NIMsLastRow = Worksheets("NIMSCarrierCount").Cells(Rows.Count, 1).End(xlUp).Row
NIMsLastCol = Worksheets("NIMSCarrierCount").Cells(1, Columns.Count).End(xlToLeft).Column
AudLastRow = Worksheets("Audit-NIMS vs Site Topology").Cells(Rows.Count, 1).End(xlUp).Row
ColLtr = Replace(Cells(1, NIMsLastCol).Address(True, False), "$1", "")
For i = 2 To NIMsLastCol
For j = 1 To AudLastRow
On Error Resume Next
tempin = Worksheets("Audit-NIMS vs Site Topology").Cells(j, 1).Value
temp3 = Application.WorksheetFunction.VLookup(tempin, Worksheets("NIMSCarrierCount").Range("A" & 1 & ":" & ColLtr & NIMsLastRow), i, False)
If IsError(temp3) Then
Cells(j, i).Value = "NA"
Else
Cells(j, i).Value = temp3
End If
Next j
Next i
Try the following:
Option Explicit
Sub test()
Dim NIMsLastRow As Long
Dim NIMsLastCol As Long
Dim temp3 As Variant 'see change here
Dim tempin As String
Dim ColLtr As String
Dim AudLastRow As Long
NIMsLastRow = Worksheets("NIMSCarrierCount").Cells(Rows.Count, 1).End(xlUp).Row
NIMsLastCol = Worksheets("NIMSCarrierCount").Cells(1, Columns.Count).End(xlToLeft).Column
AudLastRow = Worksheets("Audit-NIMS vs Site Topology").Cells(Rows.Count, 1).End(xlUp).Row
ColLtr = Replace(Cells(1, NIMsLastCol).Address(True, False), "$1", "")
Dim j As Long
For j = 1 To NIMsLastRow
On Error Resume Next
tempin = Worksheets("Audit-NIMS vs Site Topology").Cells(j, 1).Value
temp3 = Application.Match(tempin, Worksheets("NIMSCarrierCount").Range("A1:A" & NIMsLastRow), 0)
If IsError(temp3) Then
Cells(j, 2).Resize(1, NIMsLastCol - 1) = "NA"
Else
Cells(j, 2).Resize(1, NIMsLastCol - 1).Value = Worksheets("NIMSCarrierCount").Range("B" & temp3 & ":" & ColLtr & temp3).Value
End If
On Error GoTo 0
Next j
End Sub
I have multiple rows which are sometimes in order and sometimes not.
Out of rows which are in order, I would need to create a range, which are not in order just to copy the number.
The thing is, the most rows in order can be even 20.
For example cells:
1
3
5
6
7
8
9
10
13
14
15
There would be:
1
3
5-10
13-15
Is it possible to code it?
Thanks
Assuming your data starts with A1.... and
required results will be printed at C column.
Try with below code
Sub test()
Dim i As Long, lastrow As Long, incre As Long
Dim startno As Variant
Dim endno As Variant
incre = 1
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To lastrow
If Cells(i, 1) = (Cells(i + 1, 1) - 1) Then
startno = Cells(i, 1)
Do While Cells(i, 1) = (Cells(i + 1, 1) - 1)
endno = Cells(i + 1, 1)
i = i + 1
Loop
Cells(incre, 3) = "'" & startno & "-" & endno
incre = incre + 1
Else
Cells(incre, 3) = Cells(i, 1)
incre = incre + 1
End If
Next i
End Sub
if you want the address of all consecutive ranges you could use:
Option Explicit
Sub main()
Dim rangeStrng As String
With Worksheets("MyRowsSheet") '<--| change "MyRowsSheet" with your actual sheet name
rangeStrng = .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants).Areas.Parent.Address(False, False)
End With
End Sub
if you want only the rows range then you could use:
Option Explicit
Sub main2()
Dim rng As Range
Dim rowsRangeStrng As String
With Worksheets("MyRowsSheet") '<--| change "MyRowsSheet" with your actual sheet name
For Each rng In .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
If rng.Rows.Count = 1 Then
rowsRangeStrng = rowsRangeStrng & rng.Rows(1).Row & ","
Else
rowsRangeStrng = rowsRangeStrng & rng.Rows(1).Row & "-" & rng.Rows(rng.Rows.Count).Row & ","
End If
Next rng
End With
If rowsRangeStrng <> "" Then rowsRangeStrng = Left(rowsRangeStrng, Len(rowsRangeStrng) - 1)
End Sub
If I understood your question correctly, you are not looking to address a range, but rather want an output table. This code below should provide you with just that. My input numbers are in column A, and the output is in column B.
Sub sequentials()
Dim tws As Worksheet
Dim tmpRowA, tmpRowB As Integer
Dim seq() As Long
Dim frA, frB, lrA As Integer 'firstrow col A, col B, lastrow of data
Set tws = ThisWorkbook.Worksheets("Sheet1")
frA = 2
frB = 2
lrA = tws.Range("A1000000").End(xlUp).Row
'Input in column A, Output in column B
'Headers in Row 1
ReDim seq(0 To lrA - 1)
seq(0) = -2
seq(1) = tws.Range("A" & frA).Value
tmpRowA = frA
tmpRowB = frB
tws.Range("B" & frB & ":B" & lrA).NumberFormat = "#"
For r = frA + 1 To lrA
If r = 23 Then
r = 23
End If
With tws
seq(r - 1) = .Range("A" & r).Value
If seq(r - 1) = seq(r - 2) + 1 Then
If r = lrA Then
.Range("B" & tmpRowB).Value = .Range("A" & tmpRowA - 1).Value & "-" & seq(r - 1)
End If
Else
If seq(r - 2) = seq(r - 3) + 1 Then
.Range("B" & tmpRowB).Value = .Range("A" & tmpRowA - 1).Value & "-" & seq(r - 2)
Else
.Range("B" & tmpRowB).Value = seq(r - 2)
End If
tmpRowB = tmpRowB + 1
tmpRowA = r + 1
If r = lrA Then
.Range("B" & tmpRowB).Value = seq(r - 1)
End If
End If
End With
Next r
End Sub
Proof of concept: