How to define name of a range with duplicate values in VBA? - vba

I'd like to know how to define name for a range with duplicate values and have it automatically run through a the whole table. Please see the table below.
A B
1 CODE Display
2 100 000
3 100 000
4 100 010
5 100 020
6 100 030
7 100 100
8 110 000
9 110 010
10 110 020
11 110 030
12 110 100
13 110 101
14 110 200
15 110 204
16 110 208
17 110 209
Now, I know that I can use the method Names.Add to define. However, my problem is to be able to get the name range of the duplicate values in Column A as shown in the snippet.
I want to define name of Range B2:B7 as "t_100", for example. What I can think of is to get the address range from A2:A7, offset 1 column, then use the Names.Add method to refers to B2:B7. And since A2:A7 range contains only "100", it begs the question how to get the range of a continues duplicate values.
That's my initial thought.

Filtering on a known value and then referencing the .SpecialCells(xlCellTypeVisible would be a good method of gathering the cells with duplicate column A values together. Counting to see if any particular value was the first occurrence of that value as you walked down the column would ensure that you were not unnecessarily repeating actions.
Sub t_000_name_table()
Dim rw As Long, val As Variant
With ActiveSheet
With .Cells(1, 1).CurrentRegion
.AutoFilter
For rw = 2 To .Rows.Count
val = .Cells(rw, 1).Value
If Not CBool(Application.CountIf(.Cells(1, 1).Resize(rw - 1, 1), val)) Then
.AutoFilter field:=1, Criteria1:=val
With .Offset(1, 1).Resize(.Rows.Count - 1, 1)
.SpecialCells(xlCellTypeVisible).Name = Format(val, "\t\_000")
End With
.AutoFilter field:=1
End If
Next rw
.AutoFilter
End With
End With
End Sub
I've used a shorthand name declaration statement which replies heavily on the defaults. It should come in as workbook scope and absolute cell range addressing for the Refers to:. If you wish to alter the scope or other property, you can use the MSDN VBA reference for Name Object .Add.
This should work well for non-contiguous ranges as well.

Related

How to create a named range of cells based on value of another column in excel vba

I have the following sheet of data
Channel Quarter Week value
a 1 1 5811
a 1 2 199
a 1 1 8111
a 2 2 881
a 2 1 124
b 2 2 1991
I need to update the value column based on the combination of Channel and Quarter columns (I have created a helper column which would serve as a key)
Helper Channel Quarter Week value
a1 a 1 1 5811
a1 a 1 2 199
a1 a 1 1 8111
a2 a 2 2 881
a2 a 2 1 124
b2 b 2 2 1991
From my VBA form, I have a created a dictionary with the names in these helper columns like
cellDict = [a1:= 1000, 2:= 2000, b1:= 500 etc]
Now I want to update the value of 'value' column on the basis of this dictionary in such a way that the cells corresponding to a1 be updated with 1000/3 each (As the number of rows for a1 is 3) and similarly cells corresponding to a2 be updated with 1000/2 each.
The attempt was to create a named range for value column with separate helper names and then iterate over each named range and update the cell value by taking the total value and dividing it by count of that named range
So, my question is how do I create a named range with value column on the basis of helper column so that I have (Assuming value is column V)
Range("a1") = V1:V3
Range("a2") = V4:V5 etc..
I think you can do this without named ranges in a lot easier way.
First of all, I assume the rows are sorted by your helper column!
Just find the first a1 in the helper column eg. with find method and get its row number as rowStart.
Then use WorksheetFunction.CountIf Method to count the occurrence of a1 in the helper column as rowCount. So rowEnd = rowStart + rowCount - 1.
Now you can loop like …
Dim iRow As Long
For iRow = rowStart to rowEnd
'use your dictionary and rowCount to replace values here.
'Example for accessing the cell:
Worksheets("MySheet").Cells(iRow, "E").Value = ""
Next iRow

VBA: Max of 2 columns gets sent to third column

I need to get the maximum value from two columns and send it to a third column. The columns are all uniform in size but sometimes the sizes will be different, however they will all start in the same cell. For example:
5 8 -
6 2 -
6 5 -
The column with the dashes would need to find the maximum between the other two, and the finished project would look like
5 8 8
6 2 6
6 5 6
I tried recording a macro but it used ActiveCell which isn't good. I want The two columns to be, say, anything starting from C10 that has a value, and everything starting in D10 that has a value, and the max values sent to E10.
Here's what I recorded, trying to just fill the destination cell with the formula:
ActiveCell.FormulaR1C1 = "=MAX(RC[-2],RC[-1])"
Selection.AutoFill Destination:=Range("E10:E300"), Type:=xlFillDefault
Here is a good alternative of your code, just make sure to declare the first column that you compare with. The example works with the first 10 cells of Column A:
Option Explicit
Public Sub SelectMax()
Dim rngRange As Range
Dim rngCell As Range
Set rngRange = Range("A1:A10")
For Each rngCell In rngRange
'Without visible formula in Excel:
rngCell.Offset(0, 2) = WorksheetFunction.Max(rngCell, rngCell.Offset(0, 1))
'With visible formula in Excel
rngCell.Offset(0, 2).FormulaR1C1 = "=MAX(RC[-2],RC[-1])"
Next rngCell
End Sub
The example makes a visible formula in Excel. If you want to ignore the formula, remove the line after the second comment and the formula will not appear.

Loading an Excel file into SQL: Some blank cells are blank, others are NULL?

Pretty much the topic.
So in my Excel file I may have a column like this:
COLUMN
======
100
50
25
100
75
100
100
*Note that the gap rows are actually blank rows.
But when I load the file into SQL, I get something like this:
COLUMN
======
100
50
25
100
NULL <-- Note that it was blank in Excel
75
100
100
How can I format the file in Excel so that it is just consistent with one way? Thanks!
If the cells actually contain zero-length strings, you can quickly turn these into truly blank cells by sweeping through them with Range.TextToColumns method.
Sub strip_zero_length_string()
Dim c As Long
With Sheets("Sheet1").Cells(1, 1).CurrentRegion
.Cells = .Cells.Value
For c = 1 To .Columns.Count
.Columns(c).TextToColumns Destination:=.Cells(1, c), _
DataType:=xlFixedWidth, FieldInfo:=Array(0, 1)
Next c
End With
End Sub

Compare two sheets and find differences, copy to third sheet

I have been trying to compare two sheets. The sheets are versions, one made in August, the other in September. In Sheet1, column C I have a unique ID that could also be in sheet 2, but could also be not present. On the other hand, I could have NEW ID's in sheet 2, that are not present in Sheet1.
I am trying to:
Identify IDs not in "other" sheet, copy entire row to sheet3
Check if C-column value exists in other sheet, then it has to find
the differences in THOSE two rows, 12 columns out
Example, in sheet1:
ID Jan Feb Mar Apr May
14578596 125 125 125 0 10
22345697 10 10 10 10 20
12563654 150 150 75 75 75
85745896 890 890 890 890 790
and in sheet 2:
ID Jan Feb Mar Apr May
14578596 125 125 125 0 10
12563654 150 150 75 75 75
85745896 890 890 790 890 790
87544545 0 0 0 0 10
In sheet 3, it should copy over the newly added ID 87544545 and all the values in the following columns. It should copy over the ID's 22345697 entire row as well, as being non-existing in the other sheet is considered a difference.
For the others, that exist in both sheets, it should Take "Jan-Jan" and return the difference value. So it should lookup if "ID" exists in other sheet, if it does, compare the Jan-Feb-Mar with each other. Note that ID's are NOT in the same position in the sheets. With ID 85745896 it would return:
ID Jan Feb Mar Apr May
85745896 0 0 100 0 0
I have tried to look at topics such as
Compare data from 2 sheets and find mismatches
and
Check if two rows are the EXACT SAME in MS Excel but can't seem to make them work for my challenge here.
Sub compare()
For i = 1 To last_cell_mainSheet
For j = 1 To last_cell_sheet2
If Worksheets("main_sheet").Range("a" & i).Value = Worksheets("sheet2").Range("a" & j).Value
Then
Worksheets("main_sheet").Range("C" & i).Value = Worksheets("sheet2").Range("b" & j).Value
End If
Next j
Next i
End Sub
The easiest way to solve this is with excel formulas. (if you only want to do it once, or occasionally.) If you need to repeat if often (or have massive amounts of data) use VBA>
You will need to find out about VLOOKUP and IF and ISNA.
Essentially you can add a column to both sheets that looks up the ID in the other sheet and returns Y or N if it is found or not.
You formaula will be something like:
=IF(ISNA(VLOOKUP(MyIDCell,TheTableInTheOtherSheet, 1, false)),"N","Y")
TheTableInTheOtherSheet is a range starting with the ID column (and only neededing one column)
Do the Jan-Jan bit.
Again use a vlookup formula column on sheet1 to get the value of Jan from sheet2. The add a formula to compare them.
You formula will be something like:
=VLOOKUP(MyIDCell,TheTableInTheOtherSheet, 2, false)
TheTableInTheOtherSheet is a range starting with the ID column (and two columns wide)
2 get the second column value
Once you have the data you can sort or filter to reduce the list ot thsoe you need to copy to sheet 3 (best to sort). Then copy and paste them.

vba find values in sheet2 to fit values in sheet1

I have two data ranges:
DataRange is the range in Sheet1 (something like A16:F30)
HoursDataRange is the range in Sheet2 (something like A17: G90).
I need to loop via DataRange rows, take values from row, do some manipluation (like adding those data into seeprate wrokbook), as well, as take corresponding cell in column A and find all rows in Sheet2 (HoursDataRange). I do not know how to do the last thing - find all matching rows in Sheet2.
I am not familiar with VBA yet, so I have read that I can use AutoFilters to filter HoursDataRange or lookups or loops.
What is the best solution?
As I am programmer, I do understand loops, but I am not so familar with VBA.
Here is a shortenedn example of Sheet1:
NAV Nr. CompName VAT Nr. Adress Name Type
101 Int1 1 Address1 Au 1
103 Int2 2 Address2 De 1
210 Int3 3 Address3 Es 1
212 Int4 4 Address4 Sw 1
310 Int5 5 Address5 Fi 1
345 Int6 6 Address6 Fr 1
And here is shortened example of Sheet2:
Key Acc Key NAV Nr. Client EUR
3 Bu STA BU 212 Cl1 25,00
4 Bc STA BC 101 Cl2 25,00
1 Bu STA BU 212 Cl1 25,00
2 Bc STA BC 101 Cl2 25,00
So, you see the column Nav Nr. is in both sheets and there are matching values. We can assume that each Nav Nr. occures only once in Sheet1 and I am already looping via Sheet1 DataRange. However, in Sheet2 given Nav Nr can appear from 0 to N times. So I need to find all matching rows (I need to find the whole row and not just cell, because I need manipulations for the all values in rows, for example, Later I would have to sum up EUR column by Nav Nr. and create PDF, but this woudl be later staff. FOr now I just want to know how to find those matching rows.
You'll just want to do exactly as you describe. Here's an example:
Sub renathy()
Dim Cdata As Range
Dim Chour As Range
For Each Cdata In Sheets("Sheet1").Range("A:A")
If Cdata <> "" Then
For Each Chour In Sheets("sheet2").Range("A:A")
If Cdata = Chour Then
'do stuff
End If
Next
End If
Next
End Sub