Invalid Outside Procedures and Syntax in VBA/Excel - vba

I'm new to programming/coding, and I'm having problems with my code.
I'm currently trying to write a macro in excel that will go through an excel document and insert a partially blank row on duplicate columns.
I'm currently using Excel 2010
So for example if the excel sheet contained:
Column A, Column B, Column C, Column D, Column E
1 Apples 1 40 Blue
1 Bananas 2 50 Red
1 Oranges 3 60 Pink
2 Cherries
3 Kiwis
Then the script would sort it into:
Column A, Column B, Column C, Column D, Column E
1 Apples 1 40 Blue
1 Bananas
1 Oranges
2 Cherries 2 50 Red
3 Kiwis 3 60 Pink
Thus sorting the data by Column A and C while also creating blank spaces in C, D, and E if the value in Column A does not equal the value found in Column C.
My code so far:
Sub Main()
Dim a As Long, c As Long
Dim objRange As Range
Dim strCIDCol1 As String, strCIDCol3 As String
a = 1 'row counter for Column A
c = 1 'row counter for Column C
Do Until ActiveSheet.Cells(a, 1) = ""
'sets the loop to run until A1 is blank
strCIDCol1 = ActiveSheet.Cells(a, 1)
'sets the value of A1 as CIDCol1
strCIDCol3 = ActiveSheet.Cells(c, 3)
'sets the value of C1 as CIDCol3
If (strCIDCol1 <> strCIDCol2) Then
'runs until A(a) and C(c) are not equal
Set objRange = ActiveSheet.Cells(c, 3).Range(Cells(c, 3), Cells(c, 5))
objRange.Activate
'Selects Columns C, D, and E
objRange.Insert (xlShiftDown)
'Inserts "Shift Row Down"
strCIDCol1 = ActiveSheet.Cells(a + 1, 1)
'moves on
End If
a = a + 1 'adds 1 to counter to move to next row
c = c + 1 'adds 1 to counter to move to next row
Loop
End Sub
I keep getting "invalid outside procedure" errors on
a = 1
c = 1
and
a = a + 1
c = c + 1
I'm also getting a 1004 error "insert method of range class failed" on
objRange.Insert (xlShiftDown)
I have no idea what I'm doing with objRange. I saw the code online and tried to adapt it to fit what I needed. The way it was explained was that objRange was used to highlight the selected areas Column C, D, and E in this case. And then Insert would shift the cells down by inserting a row above the highlighted cells.

If (strCIDCol1 <> strCIDCol2) Then
You have never defined strCIDCoL2. I think you meant str CIDCol3 here.
Set objRange = ActiveSheet.Cells(c, 3).Range(Cells(c, 3), Cells(c, 5))
This is wrong syntax for setting range. Try using:
Set objRange = Range(ActiveSheet.Cells(c,3).Range(Cells(c,3), Cells(c,5)))
Also are you sure about using objRange.activate instead of objRange.select ?

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

Generate table by VBA in Excel

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.

Copy data in a row and spread them on separated columns

My row data is as follows:
Column A
Datapoint1
Datapoint2
Datapoint3
Datapoint4
I can transpose, but the result will be like this.
Column A Column B Column C
Datapoint1 Datapoint2 Datapoint3
What I am trying to achieve is
Column A, Column B, Column C, Column D, Column E, Column F, Column G
Datapoint1, empty, empty, empty, empty, empty, Datapoint 2
Datapoint 3 would appear in Column M. There are 5 columns in between.
There are about 3,000 Datapoints that need to be transposed into columns (separated by 5 columns in between).
You can use this function below. I tested it with 3000 items and it ran almost instantly. The problem is after 2732 items, you run out of rows as the Excel maximum column number is 16,384 as of Office 2013 (see Office 2010 Excel Specifications and Office 2013 Excel Specifications). In the code below, once it passes that, it simply clears out the remaining rows and prints a statement to the debug window. Handle this how you want.
Sub TransposeIt()
Const maxColumns As Integer = 16384
On Error GoTo er
Application.ScreenUpdating = False '' shut off screen updating to speed up if possible
Dim x As Range
Set x = Range("A1", Range("A1").End(xlDown)) '' Selects all cells until there is a blank
'' if you might have blanks, then calculate the last row manually
Dim c As Range, col As Integer, val As Variant
For Each c In x.Cells
val = c.Value '' so as not to overwrite A1
c.Value = ""
col = (c.row - 1) * 6 + 1
If col > maxColumns Then '' max number of rows we can actually use is 2732
Debug.Print "Out of Columns, clearing Row " & c.row '' do what you want at this point
Else
Cells(1, col).Value = val
End If
Next
ex:
Application.ScreenUpdating = True
Exit Sub
er:
Debug.Print "An Error has occured: " & Err.Description
Resume ex
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

Comparing the cell values and printing the count in Excel using a formula or function?

I need a formula or function which is going to fulfill my below mentioned need. I have a excel data of around 11000 rows and data looks somewhat like in Column A:
Now in column B i want the result to be printed like it mentioned below: which literally means it should count the values present in column A and print it in the column B, I don't need to repeat count:
Column A Column B
PC-101 1
PC-101 1
PC-102 2
PC-102 2
PC-103 3
PC-104 4
PC-106 5
PC-107 6
PC-104 4
PC-106 5
PC-106 5
I tried with the "count" series formulas but the result was null.
Even i wrote the macro as given below( which i got from stackoverflow) but even it is printing the repeating count:
Sub CountOccurence()
' Reference: Microsoft Scripting Runtime
Application.ScreenUpdating = False
Set oDict = New Dictionary
Dim wS As Worksheet
Dim r As Integer, rLast As Integer
Set wS = Sheet1
rLast = wS.Cells(1, 1).CurrentRegion.Rows.Count
For r = 3 To rLast Step 1
If Not (oDict.Exists(wS.Cells(r, 1).Value)) Then
oDict.Add wS.Cells(r, 1).Value, 1
Else
oDict.Item(wS.Cells(r, 1).Value) = oDict.Item(wS.Cells(r, 1).Value) + 1
End If
wS.Cells(r, 2).Value = oDict.Item(wS.Cells(r, 1).Value)
Next r
Set oDict = Nothing
Application.ScreenUpdating = True
End Sub
Can anyone help me regarding this? Thanks in advance.
I tried with the "count" series formulas but the result was null.
A simple Excel formula can do this.
Put 1 in Cell B1 and then put this formula in cell B2 and pull it down.
=IF(COUNTIF($A$1:$A2,A2)>1,VLOOKUP(A2,A:B,2,0),B1+1)
Assuming that your data in column a is sorted, you can simply place 1 in B2 this formula in B3 and copy it down:
=IF(A2<>A3,B2+1,B2)
:-)