Generate table by VBA in Excel - vba

I'm new in VBA. I hope that this is not a difficult question for you.My problem:
I have TEXT in column A and NUMBER in column B. Like this:
Column A Column B
TEXT 1 3
TEXT 2 2
TEXT 3 3
..... ...
I need to auto-generate a table in other sheet which has two columns. First contains the text which repeats n times (NUMBER in column B) and then the next text from Column A. In the second column of this table I need number from 1 to NUMBER. like this:
Column A Column B
TEXT 1 1
TEXT 1 2
TEXT 1 3
TEXT 2 1
TEXT 2 2
TEXT 3 1
TEXT 3 2
TEXT 3 3
.... ....
Then I have to post-process this table, but I know how to make it. I don't know how to generate the table.

Expanding on my comment:
Sub MakeTable()
Dim i As Long, j As Long, k As Long, m As Long, n As Long
Dim t As String
Dim ws As Worksheet
Sheets(1).Activate
Set ws = Sheets(2)
n = Cells(Rows.Count, 1).End(xlUp).Row()
k = 1
For i = 1 To n
t = Cells(i, 1).Value
m = Cells(i, 2).Value
For j = 1 To m
ws.Cells(k, 1).Value = t
ws.Cells(k, 2).Value = j
k = k + 1
Next j
Next i
End Sub
This assumes that the original data is in Sheet1 and you are transferring it to Sheet2, and that the data begins in row 1. Adjust accordingly if those assumptions are false. The way I determine the last cell in column A that has data is an important idiom in Excel VBA and should be mastered.

Related

For loop and If statement Excel VBA

I would really appreciate if you could help me out with this problem. I am trying to get vba to provide me all the numbers in increment of 1 from 1 to a value in cell G1 and populate it into column C. Afterwards, I want vba to check each cell in column C starting from C1 to see if the value is greater than a number and to populate into the column next to it( Column D1 onwards )
For example, cell G1 has the number 5.
So, I should see the following in column c , which are the 1, 2,3,4,5 and in column D I should see only the value for cells greater than say 3. So that means only the value 4, and 5 is populated in columnn D.
I would appreciate any help as I am quite new to VBA and am trying to get a hang of it.
Thnx.
Give this a try:
Sub elyas()
Dim i As Long, MagicNumber As Long
Dim k As Long
MagicNumber = 3
k = 1
For i = 1 To [G1]
Cells(i, "C").Value = i
If i > MagicNumber Then
Cells(k, "D").Value = i
k = k + 1
End If
Next i
End Sub

Removing loops to make my VBA macro able to run on more data

in my data there are more than a thousand different six digit numbers that are reoccurring in no specific pattern. I need to find all six digit codes that exist in column A and for each number. For example 123456, then find summarize the value in column B for every row that has 123456 in column A. My code is not very effective but the runtime is not a problem if I run with only 10 rows. However, in the real data sheet there are 80 000 rows and my code will take to much time. Can someone help me edit my code but removing certain loops within loops or some stop conditions. I'm new to VBA and can't do it myself in the limited time I have.
Sub Test2()
Dim summa As Long
Dim x As Long
Dim condition As Boolean
Dim lRows As Long
Dim k1 As Integer
Dim i As Long
x = 1
Worksheets("Sheet1").Activate
For i = 100000 To 999999
k1 = 1
lRows = 10
condition = False
While k1 <= lRows
If Cells(k1, "A").Value = i Then
condition = True
End If
k1 = k1 + 1
Wend
If condition = True Then
Cells(x, "F").Value = Application.SumIf(Range("A:A"), CStr(i), Range("B:B"))
Cells(x, "E").Value = i
x = x + 1
End If
Next i
MsgBox "Done"
End Sub
You don't need VBA for this task. Follow these steps.
Insert a blank column C in a copy of the original data sheet.
Insert a SUMIF formula, like =SUMIF(A:A, A2, B:B) in C2 and copy all the way down.
Now all items 123456 will have the same total in column C
Copy column C and Paste Values (to replace the formulas with their values).
Delete column B.
Remove duplicates.

Excel: Add sequential column using VBA

I am trying to add a column in Excel using VBA for sequential numbers.
Column 1 Column 2
Xyz Data
Zyx Data
Yzx Data
I wish to add a numbered sequence to column 1.
Column 1 Column 2
1 Xyz Data
2 Zyx Data
3 Yzx Data
Also I would like to control where the sequence starts from. For example from 5 onwards....
Column 1 Column 2
5 Xyz Data
6 Zyx Data
7 Yzx Data
Any help is appreciated. Thanks
If you are working on Row 1 to 100 of column A, you could try this:
Dim startNum As Long
Dim offNum As Long
Dim i As Long
Let startNum = InputBox("Start from:")
Let offNum = startNum - 1
For i = 1 To 100
ThisWorkbook.Sheets(1).Cells(i, 1).FormulaR1C1 = "=Row(R" & i & "C1) + " & offNum
Next
Sub enumeration()
Dim j, k As Integer
'determine the number to start with
j = Application.InputBox("What number should I start with?")
'determine how many rows are there to enumerate
k = Cells(1, 2).CurrentRegion.Rows.Count
For i = 0 To k - 2
Cells(i + 2, 1).Value = i + j
Next i
End Sub

Excel- Merge two worksheets with different text data

I have two excel worksheets that have the same column and row information.
However, one of the sheets contains blank cells where the other document has text information (but both spreadsheets are too large to know exactly where one contains info and the other doesn't)
I want to merge the two worksheets such that the final "Master Worksheet" contains all the information contained in both worksheets.
I want to get all the information from: $T$5381:$AP$5400 of worksheet "B" and paste it into the same range in worksheet "A"
I have tried using the 'Consolidate' function in Excel, but that appears to only work if the data is numeric. I have tried to select all the data from one workbook, highlight the blanks of the other workbook and paste, but Excel won't let me because the "copy area and paste area are not the same size or shape."
Does anybody have any ideas?
So like this
Sheet 1
A B C
1 A C
2 Z
3
Sheet 2
A B C
1 X
2
3 L O R
Master Sheet
A B C
1 A C X
2 Z
3 L O R
Try something like this;
note: you may need to adjust the sheet names
Sub MergeData()
Application.ScreenUpdating = False
Dim A, B, C, i&, j&
Set A = Sheets("Sheet1")
Set B = Sheets("Sheet2")
Set C = Sheets("Sheet3")
For i = 1 To A.UsedRange.Rows.Count + 10
For j = 1 To A.UsedRange.Columns.Count + 10
C.Cells(i, j) = IIf(IsEmpty(A.Cells(i, j)), B.Cells(i, j), A.Cells(i, j))
Next j
Next i
Application.ScreenUpdating = True
End Sub

how to paste different column names in particular rows in single column excel

How to paste different column names in particular rows in excel through Macro.
For Example :
I am having 10 different columns each column has 10 rows of data.
i have done coding for pasting those 10 column values in a single column (ex : A Column) (Hence 100 rows of data -> 10 rows *10 columns) but i need to paste all those 10 column names in B column in rows corresponding to the column values.
Before My Coding :
Name 1 Name 2 Name 3 Name 4
A E H K
B F I L
C G J M
After My Coding :
Name 1 Name 2 Name 3 Name 4
A
B
C
E
F
G
H
I
J
K
L
M
What I am expecting now !!
Name Name Category
A Name 1
B Name 1
C Name 1
E Name 2
F Name 2
G Name 2
H Name 3
I Name 3
J Name 3
K Name 4
L Name 4
M Name 4
Any excel expert please help me out. Thanks in advance
Try this. I've tested the following code on your example table and it seems to be working.
Sub VijayRamRaja()
Dim cols As Integer
cols = ActiveSheet.UsedRange.Columns.Count
For iCol = 2 To cols
Dim tHead, tData, tNewS
Set tHead = Cells(1, iCol)
Set tData = Range(Cells(2, iCol), tHead.End(xlDown))
tNewS = Range("A1").End(xlDown).Offset(1, 0).Address
tData.Cut Range(tNewS)
For Each iCell In Range(Range(tNewS), Range(tNewS).End(xlDown))
iCell.Offset(0, 1).Value2 = tHead.Value2
Next iCell
tHead.Clear
Next iCol
For Each iCell In Range("B1", Range("B1").End(xlDown).Offset(-1, 0))
iCell.Value2 = Range("A1").Value2
Next iCell
ActiveSheet.Rows(1).EntireRow.Delete
End Sub