Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all 50040 articles
Browse latest View live

How to change the number format?

$
0
0
Hi friends,
The number in column in 'AN' is in general number format.
The number format of a number in column 'AN' should be change to custom
Code:

.NumberFormat = "[$-4000439]0"
after concatenate in column 'AP'.

The following code is working fine for concatenate but I want an amendment to change a number format custom after concatenate.
Code:

Private Sub Worksheet_Change(ByVal Target As Range)

'CONCATENATE column AP

var = Array("AN", "AO")

Application.EnableEvents = False
vTemp = ""
For lngArr = LBound(var) To UBound(var)
vTemp = vTemp & Cells(Target.Row, var(lngArr)).Value & " "
Next lngArr
vTemp = Replace(Trim(vTemp), "-", " ")
vTemp = Replace(Trim(vTemp), " ", " ")
Cells(Target.Row, 42).Value = vTemp

Application.EnableEvents = True
end sub

any help will be highly appreciated.

Thanking you in anticipation.

VBA for open comment by clicking cell, formatting, paste and clear all from clipboard

$
0
0
I've been googling this for awhile. Sometimes I think I get pieces of the code but seem to ulimately fall short. Anything you can do to help would mean so much to me. Here is what I'm looking for.

When I have exactly four items in my Office Clipboard I want to Paste All into the comment of the selected cell then Clear All inside the clipboard. I would prefer never to have to open the comment myself.

I have some specific conditions for the paste. Before pasting, if there is already text inside the comment I want it all deleted then to be cursored at the top of the blank comment, then Paste All. If there is no existing comment I want to insert one, delete the comment heading, then cursor me at the top of the blank comment, then Paste All.

The Paste All needs to be bold text.

Each item is a decimal number to the tenth or hundredth. The last item will always be exactly two numbers to the tenth or hundredth. Here is an example of what the clipboard will look like :

Click an item to paste :
7.24
7.7
9.65
8.4 8.21

When I Paste All, I would like to swap the last two numbers and add a Return space between them, formatted like :

7.24
7.7
9.65
8.21

8.4

Again, any help would be deeply appreciated even if it's pieces.
Thank you!!!

Modify existing code to work with new workbook.

$
0
0
Hello,

I am new to VBA and can manage basics like change path and file names. I have attached 2 workbooks and a code below.
What code does is:

1. Make copy of AUDIOGRAMS.xlsx and number it 1.xlsx.
2. Copy data from AUDIOGRAM.xlsm and paste it to 1.xlsx to generate a chart.
3. Save workbook --> Close workbook.
4. Create 2.xlsx for next row in audiogram.xlsm.

So each row in audiogram.xlsm is used to generate charts for individual patients.

PLEASE NOTE: The code below won't work for the attached sheets. This code works with other sheet which is used to generate other workbooks for blood reports. But the concept is same.

I need to copy 43 values in a row ( Ref By EMP NO NAME DEPT. AGE *** DATE LEFT EARFT AIR 250 LEFT EARFT AIR 500 LEFT EARFT AIR 1K LEFT EARFT AIR 1.5K LEFT EARFT AIR 2K LEFT EARFT AIR 3K LEFT EARFT AIR 4K LEFT EARFT AIR 6K LEFT EARFT AIR 8K LEFT EARFT BONE 250 LEFT EARFT BONE 500 LEFT EARFT BONE 1K LEFT EARFT BONE 1.5K LEFT EARFT BONE 2K LEFT EARFT BONE 3K LEFT EARFT BONE 4K LEFT EARFT BONE 6K REMARKS LEFT EAR RIGHT AIR 250 RIGHT AIR 500 RIGHT AIR 1K RIGHT AIR 1.5K RIGHT AIR 2K RIGHT AIR 3K RIGHT AIR 4K RIGHT AIR 6K RIGHT AIR 8K RIGHT BONE 250 RIGHT BONE 500 RIGHT BONE 1K RIGHT BONE 1.5K RIGHT BONE 2K RIGHT BONE 3K RIGHT BONE 4K RIGHT BONE 6K REMARKS RIGHT EAR )

and paste it to sample woorkbook's copy created. (1.xlsx) For eg. I2 to Q2 in sample data file to M6 to M14 in sample file. Similarly for name, age , date , remarks etc. B2 in data file to AR2 in data file (43 entries) need to be copied to sample file to create charts.

I don't understand how the reference cells are set in code below.

These lines decide where to paste data i guess.

wsCard.Cells(6, 3).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 1)
wsCard.Cells(7, 3).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 2)
wsCard.Cells(6, 7).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 3)
wsCard.Cells(7, 7).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 4)
wsCard.Cells(8, 4).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 5)
wsCard.Cells(9, 3).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 6)



Thank you.

If code is misleading, please avoid the code. Simply put -

I want to copy 43 entries in a row to sample blank file, save file and create new file If there are 100 rows, 100 new workbooks will be created in the folder named 1.xlsx, 2.xlsx, 3.xlsx and so on. Only first column Sr No is not copied. Rest 43 need to get copied to their corresponding places.




Code:

Sub altfel()
Dim path As String, card As Integer
Dim wbCard As Workbook, wsCard As Worksheet
Dim wbSource As Workbook, wsSource As Worksheet
Dim lr As Long, i As Long, j As Long


Set wbSource = Workbooks("BLOODGROUPCARDENT.xlsm")
Set wsSource = wbSource.Worksheets("SOURCE DATA")

path = "D:\excelreport\BLOODGROUPCARDENT\"
Set wbCard = Workbooks.Open(Filename:=path & "BLOODGROUPCARDENT.xlsx")
Set wsCard = wbCard.Worksheets("BGCARDS")
With wsSource
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
End With

For i = 2 To lr
    For j = 1 To 28 Step 7
       
        card = card + 1
        wsCard.Cells(6, 3).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 1)
        wsCard.Cells(7, 3).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 2)
        wsCard.Cells(6, 7).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 3)
        wsCard.Cells(7, 7).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 4)
        wsCard.Cells(8, 4).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 5)
        wsCard.Cells(9, 3).Offset((card - 1) * 9) = wsSource.Cells(i, j).Offset(, 6)
       
    Next j
   
    wbCard.SaveCopyAs Filename:=path & i - 1 & ".xlsx"
    card = 0
   
Next i
MsgBox "Check the folder!"
Set wbSource = Nothing
Set wsSource = Nothing
Set wbCard = Nothing
Set wsCard = Nothing
End Sub

Attached Files

Vba to copy and paste only specific cells to a new sheet

$
0
0
Hi All

I have the below code which needs some tweaking to allow for the desired result displayed in my attached file.
Sheets("BP") will give the result of current code when macro button is pressed
Sheets("BP result wanted") is what I am trying to achieve.....Can any one assist....pretty please.
Code:

Sub Move()
Application.ScreenUpdating = False
Dim TRow As String, LastRow As Long
TRow = "20160908289"
Sheets("Temp").Activate
With Sheets("Temp")
    For v = 2 To 2000
        If Sheets("Temp").Cells(v, 2) = TRow Then
            Cells(v, 6).Value = "REFUND"
      End If
    Next
End With
Sheets("Temp").Select
LastRow = Sheets("BP").Cells(Rows.Count, 2).End(xlUp).Row
With Sheets("Temp")
        With .Range("A1").CurrentRegion
            .AutoFilter 2, TRow
            .Offset(1).Copy Sheets("BP").Range("A" & LastRow)
            .Offset(1).EntireRow.Delete xlShiftUp
        End With
        .AutoFilterMode = False
    End With
Application.ScreenUpdating = True
End Sub

Attached Files

Help Rewriting With Loops

$
0
0
Hello, I wrote some code the other day and I'm excited that it even works. It's just messy without a loop. I tried to read up on loops and I'm a little lost.

Basically, my code adds up each row and then sets each cell to 0. It will add to your total each time you hit the button.

Also, can someone throw in the code on how to 'print' out a variable, so I can see what my loops are doing.

Here is my code:
Code:

Private Sub CommandButton1_Click()
 
 


'Sets total Reps to the sum of the row
Total_Reps1 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 6 & ":N" & 6))
Total_Reps2 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 7 & ":N" & 7))
Total_Reps3 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 8 & ":N" & 8))
Total_Reps4 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 9 & ":N" & 9))
Total_Reps5 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 10 & ":N" & 10))
Total_Reps6 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 11 & ":N" & 11))
Total_Reps7 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 12 & ":N" & 12))
Total_Reps8 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 13 & ":N" & 13))
Total_Reps9 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 14 & ":N" & 14))
Total_Reps10 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 15 & ":N" & 15))
Total_Reps11 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 16 & ":N" & 16))
Total_Reps12 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 17 & ":N" & 17))
Total_Reps13 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 18 & ":N" & 18))
Total_Reps14 = Application.WorksheetFunction.Sum(Sheet1.Range("G" & 19 & ":N" & 19))




'Sets Column 14(N) to the sum of Total Reps

ThisWorkbook.Worksheets("ALL").Cells(6, 14).Value = Total_Reps1
ThisWorkbook.Worksheets("ALL").Cells(7, 14).Value = Total_Reps2
ThisWorkbook.Worksheets("ALL").Cells(8, 14).Value = Total_Reps3
ThisWorkbook.Worksheets("ALL").Cells(9, 14).Value = Total_Reps4
ThisWorkbook.Worksheets("ALL").Cells(10, 14).Value = Total_Reps5
ThisWorkbook.Worksheets("ALL").Cells(11, 14).Value = Total_Reps6
ThisWorkbook.Worksheets("ALL").Cells(12, 14).Value = Total_Reps7
ThisWorkbook.Worksheets("ALL").Cells(13, 14).Value = Total_Reps8
ThisWorkbook.Worksheets("ALL").Cells(14, 14).Value = Total_Reps9
ThisWorkbook.Worksheets("ALL").Cells(15, 14).Value = Total_Reps10
ThisWorkbook.Worksheets("ALL").Cells(16, 14).Value = Total_Reps11
ThisWorkbook.Worksheets("ALL").Cells(17, 14).Value = Total_Reps12
ThisWorkbook.Worksheets("ALL").Cells(18, 14).Value = Total_Reps13
ThisWorkbook.Worksheets("ALL").Cells(19, 14).Value = Total_Reps14

'Clear
Sheets("ALL").Range("G6:M19").Value = "0"

End Sub

Save record depending on cell values and if record already exists

$
0
0
I want to save a range of data (X8:bz8) into an array on another sheet programmatically. When the code is run, I want it to do one of three things depending on the value of two cells. I can easily express it as a formula but I'm battling to get the code right. Assume the cell values are

1. Cell "A1" (will show TRUE or FALSE), and
2. Cell "V6" (will contain a string which is a unique identifier).

Condition 1: If A1 = False and V6 already exists in a range (say C2:C5000 on another sheet), overwrite the existing data in the array with the data in X8:BZ8

Condition 2: If A1 = False and V6 does not exist in the range C2:C5000, append the data to the end of the array

Condition 3: If A1 = TRUE, show a message box saying 'the data exists and the record is closed', and do not amend or append the data from X8:BZ8 anywhere. (This should probably be the first condition tested.)

The code I have so far is:


Code:

Sub Save()

Dim sws As Worksheet, dws As Worksheet
Dim rng As Range
Dim r As Long
Dim wht
Set sws = Sheets("Admission Data Entry")
Set dws = Sheets("DatabaseAdmissions")
wht = sws.Range("v6").Value

Application.ScreenUpdating = False
sws.Range("x8:bz8").Copy
With dws.Range("c2:c5000")
  Set rng = .Find(what:=wht, LookIn:=xlValues, LookAt:=xlWhole)
  If Not rng Is Nothing Then
      r = rng.Row
  Else
      r = dws.Cells(Rows.Count, 2).End(xlUp).Row + 1
  End If
End With
dws.Range("b" & r).PasteSpecial xlPasteValues

Application.CutCopyMode = False
Range("e9").Select
Application.ScreenUpdating = True

End Sub

Any help is much appreciated.

Custom function to Extract Data from Closed workbook Help

$
0
0
Dear All,

I Have a Data folder which contains data for few years. There are sub folder for each year (like 2011, 2012, 2013) and each year folder there are sub folders for each month (like January, February, March,..) and in every month folder there are workbooks for each day (like 1.xml, 2.xml, 3.xml,...) In each workbooks there are three data columns for each hour. Also I have master workbook which contain day in the column A and hour in column B. I need to fill Columns C,D,E of the master worksheet using relevant data.
So I want to write formula to do this automatically without opening the data workbooks acoording to date and hour (column A and B in master workbook), I have attached sample data workbooks and master workbook for your kind reference herewith

http://www.mediafire.com/download/w2...hlms1/Test.rar

Please someone kindly help me to write formula for this. Thanks in advance

Ueranda

URGENT!!!!! Please help me, else i will be on Danger

$
0
0
Dear All, I need to prepare an Excel file assigned to me on my new Job. I can't make it thoroughly , seeking your expertise help to get it done. Please help! The file on attached will be a DH based sheet where , when the Month is selected then it will be blank for input data and it saved accordingly on background. it will continues on every month. After that when i again select month then it will show the saved data of that month. Also there is a conmnecting background sheet where all the data will be solved. Can you plz check the file with comments and help me ASAP!!!! Please!!!

Due to file size I need to uplaod it as Binary book.
Attached Files

If this 'a is a + 1' else 'a is a - 1'

$
0
0
Hi, Look at this, guys:
Sub BringInDiagonals()
'
' BringInDiagonals Macro
'
Application.ScreenUpdating = False
If Diagz = 0 Then
Diagz = Diagz + 1
Else
Diagz = Diagz - 1
End If
Application.ScreenUpdating = True
End Sub

Someone who knows, please tell me why this is not working (and please give me some better programming); you'll get the idea: if Diagz = 0 (to start with) make it 1 [1 higher]; if it's not (probably equals 1 already) make it 0 [1 less]. Thanks for your help, anyone!

VBA code to get the count of visible rows after filter

$
0
0
Hellp Guys,

I am trying to find a code that will return the count of visible rows after using autofilter.

I actually want to place the return value on a cell. Example: if the no of rows visible if 64 on Sheet1 and I want to place the value "64" on Cell B1 on Sheet2.

Please let me know if its possible

Thanks

Can't Change Visibility of Shapes Border Lines with Toggle Button

$
0
0
Hey Guys, I just started working with VBA and I got a problem I can't find the answear to.

This is how it should function: You click on a shape, that works like a toggle button, to change the visibility of another shapes border lines from Visible to Not visible and the other way round.
I found a code that changes the text inside a shape when you press a toggle button and adapted it to the shape in my sheet. This is how it looks like :

Code:

Sub Toggle_Click()
Dim Shp As Shape
Set Shp = Shapes("Abgerundetes Rechteck 18")
With Shp.TextFrame.Characters
If .Text = "On" Then
.Text = "Off"
.Font.Color = RGB(255, 255, 255)

Else
.Text = "On"
.Font.Color = RGB(255, 80, 255)
.Font.Bold = True

End If
End With
End Sub

If tried hours to adapt it to work the way i want but i can't find a solution, because my knowledge is way to low.
Could anybody tell me how i have to change the code to work the way i want ?
Thank you

[SOLVED] Error when creating pivot table based on user date range selection

$
0
0
Hi, I am getting the error: Run-time error '5' - Invalid procedure call or argument

at the line: ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="WOF" _
, Version:=6).CreatePivotTable TableDestination:="Who's On 1st!R2C1", _
TableName:="PivotTable3", DefaultVersion:=6

of the following code.. Whats happening is on the sheet "FLHearingsMaster" there is a table.. and there is a button to run the macro WOF - Which prompts the user for a start date and then a finish date... it filters the table (Table_FLHearings) by the date range.. (dates are in table column 6) and then moved that data to another sheet named "Who's on 1st Data" where it creates a table named "WOF" then, and this is where its causing the error, I want it to insert the pivot table, based on the table "WOF"...

does anyone know what I am doing wrong here by chance?



Code:

Sub WOF()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    'On Error GoTo TryAgain
    Dim strDate1 As String, strDate2 As String
    Dim lDate1 As Long, ldate2 As Long
    strDate1 = InputBox("From Date")
    If strDate1 = vbNullString Then Exit Sub
        If Not IsDate(strDate1) Then
            MsgBox "Non valid date"
            Run "FilterByDateRange"
        End If
    lDate1 = CDate(strDate1)
ToDate:
    strDate2 = InputBox("To Date")
    If strDate2 = vbNullString Then Exit Sub
        If Not IsDate(strDate2) Then
            MsgBox "Non valid date"
            GoTo ToDate
        End If
    ldate2 = CDate(strDate2)
    Sheets("Who's On 1st Data").Delete
    Sheets("Who's On 1st").Delete
    Worksheets.Add().Name = "Who's On 1st Data"
    Worksheets.Add().Name = "Who's On 1st"
            With Sheets("FLHearingsMaster")
                .Rows("25:25").EntireRow.Hidden = False
                .Rows("25:25").Copy Destination:=Sheets("Who's On 1st Data").Rows("1:1")
                .Rows("25:25").EntireRow.Hidden = True
                .Range("Table_FLHearings[#All]").AutoFilter Field:=6, Criteria1:=">=" & lDate1, _
                Operator:=xlAnd, Criteria2:="<=" & ldate2
                .Columns("P:Y").EntireColumn.Hidden = False
            End With
        Range("Table_FLHearings[#All]").SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("Who's On 1st Data").Range("A2")
        Sheets("Who's On 1st Data").Select
            With Sheets("Who's On 1st Data")
                .Columns("A").Delete Shift:=xlToLeft
                .Range("A1").Delete Shift:=xlToLeft
                .ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).Name = "WOF"
                .Range("A:O").Columns.AutoFit
                .Range("P:Y").ColumnWidth = 21
            End With
        Sheets("Who's On 1st").Select
        Range("A2").Select
        ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="WOF" _
            , Version:=6).CreatePivotTable TableDestination:="Who's On 1st!R2C1", _
            TableName:="PivotTable3", DefaultVersion:=6
        Sheets("Who's On 1st").Select
        Cells(2, 1).Select
        ActiveSheet.PivotTables("PivotTable3").Name = "WOF"
        Range("A2").Select
        Sheets("Who's On 1st").PivotTables("WOF").AddDataField Sheets("Who's On 1st").PivotTables( _
            "WOF").PivotFields("Hrng Date"), "Count of Hrng Date", _
            xlCount
            With ActiveSheet.PivotTables("WOF").PivotFields("Hrng Atty")
                .Orientation = xlRowField
                .Position = 1
                With ActiveSheet.PivotTables("WOF").PivotFields("Hrng Date")
                    .Orientation = xlColumnField
                    .Position = 1
                    With ActiveSheet.PivotTables("WOF").PivotFields("County")
                        .Orientation = xlRowField
                        .Position = 2
                    End With
                End With
            End With
        Rows("1:1").EntireRow.Hidden = True
        ActiveSheet.PivotTables("WOF").PivotFields("Hrng Atty").ShowDetail = False
        ActiveWorkbook.ShowPivotTableFieldList = False
        ActiveSheet.UsedRange.Borders.LineStyle = xlContinuous
        Call WOFFill
        Rows("2:2").NumberFormat = "dddd mm-dd"
        Columns("C:G").EntireColumn.AutoFit
        Cells.Select
        Range("A2").Value = "Hearing Atty  /  Date"
        Range("B3").Select
        ActiveWindow.FreezePanes = True
        Call ClearSlicers
        ActiveWorkbook.Worksheets("FLHearingsMaster").Columns("P:Y").EntireColumn.Hidden = True
        ActiveWindow.ScrollRow = 16
        Range("$B2:$O24").Select
        Worksheets("Who's On 1st").Activate
        ActiveSheet.UsedRange.Select
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Exit Sub
TryAgain:
    MsgBox "Invalid Date Range"
    Call WOF
End Sub

Sub WOFFill()
Dim rngC As Range
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
    With Worksheets("Who's On 1st")
        With .PivotTables("PivotTable2")
            .TableStyle2 = ""
            .TableRange1.Interior.ColorIndex = xlNone
            .TableRange1.Cells.HorizontalAlignment = xlCenter
            .TableRange1.Cells.VerticalAlignment = xlCenter
            .TableRange1.Columns(1).HorizontalAlignment = xlLeft
            With .TableRange1.Rows(2).Cells
                .Interior.ColorIndex = 1
                .Font.ColorIndex = 2
            End With
                With .TableRange1.Rows(lr).Cells
                    .Interior.ColorIndex = 1
                    .Font.ColorIndex = 2
                End With
            Set rngC = .DataBodyRange
            rngC.Resize(rngC.Rows.Count - 1, rngC.Columns.Count - 1).SpecialCells(xlCellTypeConstants).Interior.Color = 5263615
        End With
    End With
End Sub

[ASK] How to sum automatically every single table in excel file (over 15000 rows)

$
0
0
Hi there :),

I have a excel worksheet, it contains over 15000 rows. It's really quiet simple, i need to sum multiple rows in "NON-CHRONIC" sheet to get all of the "Subtotal" as the "CHRONIC" sheet has, one by one, but it really consume my time :(. Can someone help me please with your amazing vba programming knowledge ? Many thanks :):)

nb : you can download the attached file below to see the problem:)

Can a Macro extract cell references from a Column and paste them into a Row.

$
0
0
Please see the attached (simplified!) example.

Hundreds of Reports are filed Vertically on a worksheet.

However, I need to do a summary table on a different worksheet.

The idea is that whenever a new report is created (in the correct place under all the others) the summary table will automatically be up-dated with data from the new report.

What I would like to do is write an Excel Macro that fill in all the Cell References in the Summary Table. (Because the data is copied from a spaced out column to a compact row I don't think I can achieve this by Copying & Pasting).

Thank you for any offers of help; if you just help me get started I will do all the donkey work myself!
Attached Files

[SOLVED] How write the code for a userform to open and run

$
0
0
Having trouble getting my userform1 to open so I can select the user and have it entered into "B14" on sheet1, which is named "Proposal". Please see the attached file.

It is to select the names from "Sheet2!A2:A8" in the "ComboBox1" and place that name on "Sheet1" in "cell B14" and close. I just don't know the correct way of writing the code to make this work. Any help on this would be very appreciated.
Attached Files

Excel Memory Use

$
0
0
I open a "data" workbook and then save it many times after changes are made during the day from a master "system workbook" using VBA macros. Is there any risk of running out memory because of the multiple "data" workbook saves without closing the Data workbook? I read somewhere that excel saves a copy of a workbook each time it is saved and doesn't clear them out until the workbook is closed. Is this correct?

Is it possible for VBA to load excel data into SAS?

$
0
0
Is it accomplish-able? If yes, can someone give me some instruction how to do it?

Thanks.

Find Last Row and Insert Rows based on a number vaiable

$
0
0
I have the following code where I transfer data from Workbook A to workbook B based on the value in column B. I am having trouble getting code to find the last row and then inserting the exact number of rows that were deleted previously in the code. Right now. I have tried to set a variable (( - WorkSheetFuncion.CountIf(Range("B" & LastRow), "Shipped") - )) to hold the number of rows that will be deleted and then at the end of the code using different variations of Range.Insert and Cells.Insert with no luck.

Code:

Sub LineShipped()
Dim xNewWB As Workbook
    Dim strFile As String, strFile2 As String, strFile3 As String 'strFile = shipping report template
    Dim DataSht As Excel.Worksheet, DataSht2 As Excel.Worksheet 'DataSht = Packing List Log Sheet DataSht2 = Shipping Report
    Dim DataArr As Variant
    Dim TData As Variant
    Dim LastRow As Long, LastRow2 As Long
    Dim Rw As Long, Lrow As Long, rwc As Long
    Dim suffix As String
    Dim strFormula1 As Variant
    Dim rng1 As Range
    Dim xfrCount As Integer

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.EnableEvents = False
   
    Set DataSht = ThisWorkbook.Sheets("Order List") 'We must change this sheet name for each year
    LastRow = DataSht.Range("B" & DataSht.Rows.Count).End(xlUp).Row
    DataArr = DataSht.Range("B3:AI" & LastRow).Value
    xfrCount = DataSht.Application.WorksheetFunction.CountIf(Range("B" & LastRow), "Shipped")
   
    For Rw = LBound(DataArr, 1) To UBound(DataArr, 1)

        If DataArr(Rw, 1) = "Shipped" Then 'If the status column = shipped
       
            If IsArray(TData) Then
                ReDim Preserve TData(33, UBound(TData, 2) + 1)
                TData(0, UBound(TData, 2)) = DataArr(Rw, 1)
                TData(1, UBound(TData, 2)) = DataArr(Rw, 2)
                TData(2, UBound(TData, 2)) = DataArr(Rw, 3)
                TData(3, UBound(TData, 2)) = DataArr(Rw, 4)
                TData(4, UBound(TData, 2)) = DataArr(Rw, 5)
                TData(5, UBound(TData, 2)) = DataArr(Rw, 6)
                TData(6, UBound(TData, 2)) = DataArr(Rw, 7)
                TData(7, UBound(TData, 2)) = DataArr(Rw, 8)
                TData(8, UBound(TData, 2)) = DataArr(Rw, 9)
                TData(9, UBound(TData, 2)) = DataArr(Rw, 10)
                TData(10, UBound(TData, 2)) = DataArr(Rw, 11)
                TData(11, UBound(TData, 2)) = DataArr(Rw, 12)
                TData(12, UBound(TData, 2)) = DataArr(Rw, 13)
                TData(13, UBound(TData, 2)) = DataArr(Rw, 14)
                TData(14, UBound(TData, 2)) = DataArr(Rw, 15)
                TData(15, UBound(TData, 2)) = DataArr(Rw, 16)
                TData(16, UBound(TData, 2)) = DataArr(Rw, 17)
                TData(17, UBound(TData, 2)) = DataArr(Rw, 18)
                TData(18, UBound(TData, 2)) = DataArr(Rw, 19)
                TData(19, UBound(TData, 2)) = DataArr(Rw, 20)
                TData(20, UBound(TData, 2)) = DataArr(Rw, 21)
                TData(21, UBound(TData, 2)) = DataArr(Rw, 22)
                TData(22, UBound(TData, 2)) = DataArr(Rw, 23)
                TData(23, UBound(TData, 2)) = DataArr(Rw, 24)
                TData(24, UBound(TData, 2)) = DataArr(Rw, 25)
                TData(25, UBound(TData, 2)) = DataArr(Rw, 26)
                TData(26, UBound(TData, 2)) = DataArr(Rw, 27)
                TData(27, UBound(TData, 2)) = DataArr(Rw, 28)
                TData(28, UBound(TData, 2)) = DataArr(Rw, 29)
                TData(29, UBound(TData, 2)) = DataArr(Rw, 30)
                TData(30, UBound(TData, 2)) = DataArr(Rw, 31)
                TData(31, UBound(TData, 2)) = DataArr(Rw, 32)
                TData(32, UBound(TData, 2)) = DataArr(Rw, 33)
                TData(33, UBound(TData, 2)) = DataArr(Rw, 34)
            Else
                ReDim TData(33, 0)
                TData(0, 0) = DataArr(Rw, 1)
                TData(1, 0) = DataArr(Rw, 2)
                TData(2, 0) = DataArr(Rw, 3)
                TData(3, 0) = DataArr(Rw, 4)
                TData(4, 0) = DataArr(Rw, 5)
                TData(5, 0) = DataArr(Rw, 6)
                TData(6, 0) = DataArr(Rw, 7)
                TData(7, 0) = DataArr(Rw, 8)
                TData(8, 0) = DataArr(Rw, 9)
                TData(9, 0) = DataArr(Rw, 10)
                TData(10, 0) = DataArr(Rw, 11)
                TData(11, 0) = DataArr(Rw, 12)
                TData(12, 0) = DataArr(Rw, 13)
                TData(13, 0) = DataArr(Rw, 14)
                TData(14, 0) = DataArr(Rw, 15)
                TData(15, 0) = DataArr(Rw, 16)
                TData(16, 0) = DataArr(Rw, 17)
                TData(17, 0) = DataArr(Rw, 18)
                TData(18, 0) = DataArr(Rw, 19)
                TData(19, 0) = DataArr(Rw, 20)
                TData(20, 0) = DataArr(Rw, 21)
                TData(21, 0) = DataArr(Rw, 22)
                TData(22, 0) = DataArr(Rw, 23)
                TData(23, 0) = DataArr(Rw, 24)
                TData(24, 0) = DataArr(Rw, 25)
                TData(25, 0) = DataArr(Rw, 26)
                TData(26, 0) = DataArr(Rw, 27)
                TData(27, 0) = DataArr(Rw, 28)
                TData(28, 0) = DataArr(Rw, 29)
                TData(29, 0) = DataArr(Rw, 30)
                TData(30, 0) = DataArr(Rw, 31)
                TData(31, 0) = DataArr(Rw, 32)
                TData(30, 0) = DataArr(Rw, 33)
                TData(31, 0) = DataArr(Rw, 33)
            End If
        End If
       
  Next Rw
     
  strFile = "C:\Users\Jimmie\Desktop\Completed Fulfillment Orders.rev 1.xlsx"

    Set xNewWB = Workbooks.Open(strFile) 'Open Shipping Report Template in SharePoint
   
'    xNewWB.Activate 'Acitvate Shipping Report Template to add data

    Set DataSht2 = xNewWB.Sheets("Completed Orders")
    LastRow2 = DataSht2.Range("B" & DataSht2.Rows.Count).End(xlUp).Row + 1
    DataSht2.Range("B" & LastRow2).Resize(UBound(TData, 2) + 1, 34).Value = Application.Transpose(TData)
   
    DataSht2.Range("B" & LastRow2).Select
    xNewWB.Save
    xNewWB.Close
     
    With DataSht

        'We select the sheet so we can change the window view
        .Select

        'Set the first and last row to loop through
        Firstrow = 3
        LastRow = Cells(.Rows.Count, "B").End(xlUp).Row

        'loop from Lastrow to Firstrow (bottom to top)
        For Lrow = LastRow To Firstrow Step -1

            'We check the values in the B column
            With .Cells(Lrow, "B")

                If Not IsError(.Value) Then

                    If .Value = "Shipped" Then .EntireRow.Delete
                    'This will delete each row that contains the word Shipped
                    'in Column B, case sensitive.

                End If

            End With

        Next Lrow

    End With
   
    With DataSht
    rwc = DataSht.UsedRange.Rows.Count
    End With

    strFormula1 = "=IF(C3>0, ROW()-2,"""")"
    Cells(3, 1).Formula = strFormula1
    Cells(3, 1).Resize(rwc - 2).FillDown
   
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.EnableEvents = True

End Sub

I tried setting a variable for getting the number of rows with "Shipped" and then at the end of the code tried to use the variable. But cant figure out the correct way to use it.

Code:

Dim ShippedCnt As Integer
ShippedCnt = DataSht.Application.WorksheetFunction.CountIf(Range("B" & LastRow), "Shipped")

DataSht.Range(LastRow:ShippedCnt).Insert Shift:=xlDown
      CopyOrigin:=xlFormatFromLeftOrAbove 'or xlFormatFromRightOrBelow

I tried changing it to

DataSht.Range("A" & LastRow, "A" & ShippedCnt) but that did not work either.

I could set the sheet up to where we are always working with Row 100 as the last row if that makes it easier, then if Workbook A ever has more then 100 items in process I can update the table and the code, but I still have issues using the variable as the number of rows to insert.

Activating only the last 30 days in Excel Calendar?

$
0
0
I'm trying to create a data collection tool in a research study that will only provide the last 30 calendar days from the date it is opened. I'm using Excel Calendar, and I put the information for the date ranges in two cells on the side of the calendar, where the 30-day period start date is L4 (4,12) and end date is in L3 (3,12). I want to lock and black-out the calendar days that are not within that 30-day range to minimize confusion and error.

I pasted what I wrote into VBA so far. I practiced with 4 consecutive calendar days (all in row 6), where 2 of them should have been blacked out because they were outside of the 30-day range.

(Note: each calendar cell has two blank cells below it for data entry, which is why I want to lock and blackout 3 cells at a time for each calendar day.)

-----------------------------------------------
Code:

If ActiveSheet.Cells(6, 2).DateValue <= Cells(3, 12).DateValue And ActiveSheet.Cells(6, 2).DateValue >= Cells(4, 12).DateValue Then
ActiveSheet.Range(Cells(6, 2), Cells(7, 2), Cells(8, 2)).Locked = False
Else: ActiveSheet.Range(Cells(6, 2), Cells(7, 2), Cells(8, 2)).Locked = True
End If

If ActiveSheet.Cells(6, 2).DateValue <= Cells(3, 12).DateValue And ActiveSheet.Cells(6, 2).DateValue >= Cells(4, 12).DateValue Then
ActiveSheet.Range(Cells(6, 2), Cells(7, 2), Cells(8, 2)).Selection.Interior.Color = RGB(0, 0, 0)
Else: ActiveSheet.Range(Cells(6, 2), Cells(7, 2), Cells(8, 2)).Selection.Interior.Color = RGB(0, 0, 0)
End If


I also tried this:

Code:

If ActiveSheet.Cells(6, 3).DateValue <= Cells(3, 12).DateValue And ActiveSheet.Cells(6, 3).DateValue >= Cells(4, 12).DateValue Then
ActiveSheet.Range(Cells(6, 3), Cells(4, 3), Cells(5, 3)).Locked = False
Else: Locked = True
Selection.Interior.Color = RGB(0, 0, 0)
End If

If ActiveSheet.Cells(6, 4).DateValue <= Cells(3, 12).DateValue And ActiveSheet.Cells(6, 4).DateValue >= Cells(4, 12).DateValue Then
ActiveSheet.Range(Cells(6, 4), Cells(6, 4), Cells(6, 4)).Locked = False
Else: Locked = True
Selection.Interior.Color = RGB(0, 0, 0)
End If

If ActiveSheet.Cells(6, 5).DateValue <= Cells(3, 12).DateValue And ActiveSheet.Cells(6, 5).DateValue >= Cells(4, 12).DateValue Then
ActiveSheet.Range(Cells(6, 5), Cells(6, 5), Cells(6, 5)).Locked = False
Else: Locked = True
Selection.Interior.Color = RGB(0, 0, 0)
End If

End Sub


------------------------

None of this has worked. I'm not sure if I'm getting the DateValue Function wrong, or if I'm missing something else. I was hoping to eventually use a Loop function once I got this step figured out, but I haven't even gotten the first part down...

Any help would be much appreciated!!!

How to pass dates from userform to a macro in "ThisWorkBook"

$
0
0
Hi, I am trying to use the code found at this site in a macro I made but am unsure as to how to pass the dates entered by the user to a macro that is contained in "ThisWorkbook"

The website is here and I am attaching the sample workbook provided by the site..

http://sitestory.dk/excel_vba/calendar.htm


can anyone help me out? I know its something like declaring the variables and public and referencing them but its just not something I have any experience with....

The example just calculates the time between the two dates selected... so will remove that and just pass the two dates entered to "my" macro once the 2nd date has been entered by the user...
Attached Files
Viewing all 50040 articles
Browse latest View live