excel remove duplicates with columns array as a variable - arrays

The following code doesn't remove the rows that are duplicates - Type Mismatch Error. I have the complication that I dont know how many columns there is. Please could someone tell me what I have done wrong. Thank you
ReDim myarray(endcol - 2)
For echarrayval = 1 To endcol - 1
myarray(echarrayval - 1) = eacharracycol
Next echarrayval
remduprng = Range(Cells(1, 1), Cells(botrw, endcol - 1)).Address
ActiveSheet.Range(remduprng).RemoveDuplicates Columns:=Array(myarray), Header:=xlYes

Related

Can you populate Classic ASP Constants (CONST) dynamically?

I am wishing to replace an include file with ~ 25 declared Constants by dynamically creating those constants from a Recordset (or Array). I have tried looping through a record set and have also tried populating a 2D array and then trying to set these Constants from the array values but it seems to take what is after the "Const" literal and not the value.
If (not rsVars.Eof) Then
cCount = rsVars.RecordCount
ReDim cArray(cCount - 1, 1)
For i = 0 To cCount - 1
cArray(i, 0) = rsVars("VariableNm")
cArray(i, 1) = rsVars("Value")
rsVars.MoveNext
Next
End If
For i = 0 To cCount - 1
' Display the Array Elements - WORKS and Displays All Variables and Desired Values
Response.write "("&(i+1)&") " & cArray(i, 0) & " = " & cArray(i, 1) & "<br>"
' Attempt to set Constant Variables - this next line errors - "Name_redefined"
Const cArray(i, 0) = cArray(i, 1)
Next
You could use the server.execute method to achieve this.
But it needs a FILE path to do so.
You will need to create a file dynamically using the FileSystemObject and pass the file's full path to the server.execute method to make those variables available as constants.
ex:
constants.asp
Note
Values will be CONSTANT Across user sessions and requests.
If you need a different set of constants by user, company, date etc, you will need to change the filename to include the unique constants-.asp and execute that file.
References
https://www.w3schools.com/asp/met_execute.asp
https://www.w3schools.com/ASP/asp_ref_filesystem.asp

Counting rows based on multiple criteria

I've been searching for a way to fill in column 'H' and 'I' of the attached image.
For 'H' I need to count every time the date and date shipped out don't equal for each item. It will be something like this:
(A:A <> D:D, B:B = $G$2, E:E <> "Base Inventory")
The second equation is to calculate the average lead time for each item:
Average(E:E - D:D) where B:B = $G$2, and D:D = "Complete" while ignoring rows with blanks
The table is much larger than the image and I can't use a helper row.
Thanks.
Example Sheet
For the first part, shipping calc, I wrote a function that will compute it for you:
Public Function Shippingcalc(item1 As Range, codes As Range, date2 As Range, shipped As Range, status As Range)
Dim Count As Integer
Count = 0
For n = 1 To codes.Rows.Count
If status.Cells(n, 1).Value <> "Base Inventory" Then
If codes.Cells(n, 1).Value = item1.Cells(1, 1).Value Then
If shipped.Cells(n, 1).Value <> 0 Then
If shipped.Cells(n, 1).Value <> date2.Cells(n, 1).Value Then
Count = Count + 1
End If
End If
End If
End If
Next n
Shippingcalc = Count
End Function
You have to implement that as a macro and macro-enable the workbook.
Then the function in H would look something like:
=Shippingcalc(G2, B$2:B$10, A$2:A$10, D$2:D$10, C$2:C$10)
And you can copy that (of course updating to the full column size).
Something similar should work for the other computation.
For some reason this morning while I was taking a poop, the equation for lead time came to me. It was way simpler than I imagined and goes back to high school math.
Average = (SUM(Received date) - SUM(Shipped date)) / Total
=(SUMIFS(E:E,B:B, G2,D:D,"<>",E:E,"<>") - SUMIFS(D:D,B:B, G2,D:D,"<>",E:E,"<>")) / (COUNTIFS(B:B, Q14, D:D,"<>",E:E,"<>")+1)
Thanks for your help guys.

Excel VBA code needed to copy row if criteria is met

I have an excel document with two sheets. Sheet 1 has columns A-Q and Sheet 2 has columns A-H. What I need is a code that will copy the information in a row from sheet 1 to sheet 2 if the criteria is met. The criteria is the word "Awarded" in column L (Sheet 1).
Also is it possible to have only specific columns in the row copied?
A B C D E F G H I J K L M N
X X Awarded X X
I would like to have only columns C,D,M, and N copied from the row if the word "awarded" is in column L. This information would be copied to Sheet 2 in the following fashion
Sheet 1 Sheet 2
D --> B
C --> C
M --> D
N --> F
I hope I'm being clear. Thanks in advance and let me know if I need to clarify!+
This is the code I currently have, which works. Only problem is it copies the entire row of information into sheet 2 when I only want rows D,C,M, and N to be copied.
Sub testing()
Set a = Sheets("Sheet1")
Set b = Sheets("Sheet2")
Dim d
Dim j
d = 1
j = 2
Do Until IsEmpty(a.Range("L" & j))
If a.Range("L" & j) = "Awarded" Then
d = d + 1
b.Rows(d).Value = a.Rows(j).Value
End If
j = j + 1
Loop
End Sub
First what you should do is change your data structure. Assuming you are using Excel 2007 or later, there is a great feature called Tables. If you highlight all of your data and go to Insert->Table, select the "My Table Has Headers" checkbox, and press ok, you will see a nicely formatted table. Do that for both of the data sets on each sheet.
This is more than just pretty formatting though, it is what is called a ListObject. In your VBA code, use the following to reference it:
Dim Table1 as ListObject, Table 2 as ListObject
Dim HeaderIndex as Integer
Dim MyColumnRange as Range
Set Table1 = Sheet1.ListObjects("TableName1")
`Change the table name under Formulas->Name Manager
Set Table2 = Sheet1.ListObjects("TableName2")
HeaderIndex = Application.WorksheetFunction.Match("ColumnLHeaderName", _
Table1.HeaderRowRange, 0)
Set MyColumnRange = Table1.ListColumns(HeaderIndex).DataBodyRange
MyColumnRange.Select
At this point, the select statement is just to show you what range you are dealing with now. The HeaderIndex refers to the header sub component of the table ListObject. Using Match() will allow you to specify the name of the column header without hard coding it's position. (i.e. if your data starts in column A, the header value in column L will return HeaderIndex = 12)
Now that you know what column you want, you select the ListColumn object. Then, the DataBodyRange is used to select the range component of that object. This is the entire range in that column. You can then iterate down the list to find the data you want.
EDIT: Updated Example:
'Specify your ranges you will be copying from beforehand, adding as many as you need here.
HeaderIndex_D = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_C = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_M = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
HeaderIndex_N = Application.WorksheetFunction.Match("ColumnXHeaderName", _
Table1.HeaderRowRange, 0)
Set ColumnRange_D= Table1.ListColumns(HeaderIndex_D).DataBodyRange
Set ColumnRange_C= Table1.ListColumns(HeaderIndex_C).DataBodyRange
Set ColumnRange_M= Table1.ListColumns(HeaderIndex_M).DataBodyRange
Set ColumnRange_N= Table1.ListColumns(HeaderIndex_N).DataBodyRange
'Now, loop through each row that exists in your table. If the testing
'condition contained in MyColumnRange you previously defined is met,
'then assign the destination cell (which can be defined in the same way
'as above) equal to the lookup range's current row value (specified by i)
For i = 1 to MyColumnRange.Rows.Count
If MyColumnRange(i) = "Awarded" Then
DestinationCell1.Value = ColumnRange_D(i)
DestinationCell2.Value = ColumnRange_C(i)
DestinationCell3.Value = ColumnRange_M(i)
DestinationCell4.Value = ColumnRange_N(i)
End If
Next i

VBA Run-time error 13 - type mismatch- price time series calc not working on array

I suspect my question has a very simple answer, and yet I cannot find an answer that I understand in searching all over the site.
Ticker1 = an array of stock prices (1 to 2542)
PctChg1 = an array which I hope will ultimately hold the results of
(Price_last / Price_prev) - 1
i = counter
Code:
For i = 2 To UBound(Ticker1)
PctChg1(i, 1) = Ticker1(i, 1) / Ticker1(i - 1, 1) - 1
Next i
Something about the Ticker1(i - 1, 1) part it does not like - I have fooled around with ranges as well and cannot make sense of this. I don't know why it thinks I am dividing by a number that isn't there - if i am starting at point 2 of the array, why wouldn't I be able to reference point 1 ?
For i = 2 To UBound(Ticker1)
If Val(Ticker1(i - 1, 1)) <> 0 Then PctChg1(i, 1) = Ticker1(i, 1) / Ticker1(i - 1, 1) - 1
Next i
This works, but I still have not established why i-1 generates errors when I start the calculation on data point i=2.

VBA Error Handling Multiple times

I have an issue. I have values in a workbook that are getting read into an array. The values come from an XML list so sometimes can be numbers or text, and if they are numbers in text format ("1" for example), they need to be converted to number format, so I multiply them by 1. If the value is text "LS" for example, I am trying to use an error handler to keep the value as "LS".
I have developed the code below: It works once, but the next time I use a similar method (with 'Dummy2') the code it produces a 'Type Mismatch' error.
On Error GoTo Dummy1
For i = 1 To nrows1 - 1
If (Worksheets("Import").Cells(i + 1, 26) * 1 = "") Then
Dummy1:
Table1(i, 1) = Worksheets("Import").Cells(i + 1, 26)
Else
Table1(i, 1) = Worksheets("Import").Cells(i + 1, 26) * 1
End If
Next
On Error GoTo 0
I also tried Clearing Err after the above code with no success.
Please help!
You can use the IsNumeric function:
Sub test3()
Dim Test As Variant
Dim i As Long
For i = 1 To nrows1 - 1
Test = Worksheets("Import").Cells(i + 1, 26)
If IsNumeric(Test) Then
Table1(i, 1) = Test * 1
Else
Table1(i, 1) = Test
End If
Next
End Sub
use VAL(Worksheets("Import").Cells(i + 1, 26)) to determine if it is a Number or not. VAL will give you 0 for "LS".
You could try TryParse functions - but not sure if its available in your version of VBA

Resources