• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Need help in excel VBA data insertion|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Need help in excel VBA data insertion|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosNeed help in excel VBA data insert…
sp_PrintTopic sp_TopicIcon
Need help in excel VBA data insertion
Avatar
M M
Member
Members
Level 0
Forum Posts: 6
Member Since:
March 3, 2020
sp_UserOfflineSmall Offline
1
March 3, 2020 - 4:40 am
sp_Permalink sp_Print

I have two excel work books
1)work book1 Named as Week1 ->it has two worksheets Alerts & Tasks

2)Work Book2 named as week2 -->it has two worksheets Alerts & Tasks

Example of my Week1 file

1-Jan-2020 Alert-name Description
1-Jan-2020 Alert-name Description
2-Jan-2020 Alert-name Description
2-Jan-2020 Alert-name Description

when i loop through Week1/Week2 work book with Work sheet named Alerts
before every date i need to add the lines as

1-Jan-2020 L1 Monitoring

I was able to loop through each work book and its worksheets.

Currently i was able to loop and consolidate the data to a single sheet ,But not sure how to insert the above line before every date from the Alerts sheet

 

```Sub getDataFromWbs()

Dim wb As Workbook, ws As Worksheet
Set fso = CreateObject("Scripting.FileSystemObject")

'This is where you put YOUR folder name
Set fldr = fso.GetFolder("C:\Users\Radha\Downloads\Temp\Temp")

'Next available Row on Master Workbook
y = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1

'Loop through each file in that folder
For Each wbFile In fldr.Files

'Make sure looping only through files ending in .xlsx (Excel files)
If fso.GetExtensionName(wbFile.Name) = "xlsx" Then

'Open current book
Set wb = Workbooks.Open(wbFile.Path)

'Loop through each sheet (ws)
For Each ws In wb.Sheets
'Last row in that sheet (ws)
wsLR = ws.Cells(Rows.Count, 1).End(xlUp).Row

'Loop through each record (row 2 through last row)
For x = 2 To wsLR
'Put column 1,2,3 and 4 of current sheet (ws) into row y of master sheet, then increase row y to next row
ThisWorkbook.Sheets("sheet1").Cells(y, 1) = ws.Cells(x, 1) 'col 1
ThisWorkbook.Sheets("sheet1").Cells(y, 2) = ws.Cells(x, 2) 'col 1
ThisWorkbook.Sheets("sheet1").Cells(y, 3) = CDate(ws.Cells(x, 3)) 'col 1
ThisWorkbook.Sheets("sheet1").Cells(y, 4) = ws.Cells(x, 4) 'col 1
y = y + 1
Next x

Next ws

'Close current book
wb.Close
End If

Next wbFile

End Sub
```

sp_AnswersTopicSeeAnswer See Answer
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
2
March 3, 2020 - 9:52 am
sp_Permalink sp_Print

Please attach your workbooks

Avatar
M M
Member
Members
Level 0
Forum Posts: 6
Member Since:
March 3, 2020
sp_UserOfflineSmall Offline
3
March 4, 2020 - 1:59 am
sp_Permalink sp_Print

Hi Philip

To be frank i am not from programming background .so seeking your help.

Just got the above code searching for couple of hours and tested it which is near to my requirement.

 

Please find my excel work books attached

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
4
March 5, 2020 - 11:07 am
sp_Permalink sp_Print

No worries MM.

You say that you want to add something before every date, but your example shows something added after a date?

I've gone with adding before the date but it is easily changed.

Please see attached workbook.

Regards

Phil

Avatar
M M
Member
Members
Level 0
Forum Posts: 6
Member Since:
March 3, 2020
sp_UserOfflineSmall Offline
5
March 6, 2020 - 2:59 am
sp_Permalink sp_Print sp_EditHistory

Thanks a lot Philip for the help 🙂   

Need some help with the enhancement.

 

Currently my report is coming as below . it works well. 

01-Feb-2020 Alert L2 AlertName1 AlertDescription1
01-Feb-2020 Alert L2 AlertName2 AlertDescription2
02-Feb-2020 Alert L2 AlertName5 AlertDescription5
02-Feb-2020 Alert L2 AlertName6 AlertDescription6
01-Feb-2020 TASKS L2 taskName1 taskDescription1
01-Feb-2020 TASKS L2 taskName2 taskDescription2
02-Feb-2020 TASKS L2 taskName3 taskDescription3

I need to check for each date and add a row as below only for the Alert

01-Feb-20 Monitoring  Nagios  mail   Remedy

 

Please find the attached files with code

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
6
March 6, 2020 - 10:47 am
sp_Permalink sp_Print

Hi MM,

So after every 'Alert' row you will have a 'Monitoring' row?

See attached.

Regards

Phil

Avatar
M M
Member
Members
Level 0
Forum Posts: 6
Member Since:
March 3, 2020
sp_UserOfflineSmall Offline
7
March 6, 2020 - 9:25 pm
sp_Permalink sp_Print

Sorry it was not clear from my side.

Not to add a row for every Alert.its  before every new  date of alert

Example The requirement is as below

01-Feb-2020 Monitoring Nagios Remedy Mail
01-Feb-2020 Alert L2 AlertName1 AlertDescription1
01-Feb-2020 Alert L2 AlertName2 AlertDescription2
02-Feb-2020 Monitoring Nagios Remedy Mail
02-Feb-2020 Alert L2 AlertName5 AlertDescription5
02-Feb-2020 Alert L2 AlertName6 AlertDescription6
02-Feb-2020 Alert L2 AlertName6 AlertDescription6

 

Currently it is adding before all the alerts as below

01-Feb-2020 Alert L2 AlertName1 AlertDescription1
01-Feb-2020 Monitoring Nagios Remedy Mail
01-Feb-2020 Alert L2 AlertName2 AlertDescription2
01-Feb-2020 Monitoring Nagios Remedy Mail
02-Feb-2020 Alert L2 AlertName5 AlertDescription5
02-Feb-2020 Monitoring Nagios Remedy Mail
02-Feb-2020 Alert L2 AlertName6 AlertDescription6
Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
8
March 8, 2020 - 4:13 pm
sp_Permalink sp_Print

OK, try this.

Phil

sp_AnswersTopicAnswer
Answers Post
Avatar
M M
Member
Members
Level 0
Forum Posts: 6
Member Since:
March 3, 2020
sp_UserOfflineSmall Offline
9
March 8, 2020 - 11:33 pm
sp_Permalink sp_Print

Thanks a lot Philip 🙂 You made my work simple.

Thanks a lot for your time and continuous followup

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1514
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
10
March 9, 2020 - 12:17 pm
sp_Permalink sp_Print

no worries.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Andy Kirby, Roy Lutke, Jeff Krueger
Guest(s) 7
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
John Chisholm
vexokeb sdfg
John Jack
Malcolm Toy
Ray-Yu Yang
George Shihadeh
Naomi Rumble
Uwe von Gostomski
Jonathan Jones
drsven
Forum Stats:
Groups: 3
Forums: 24
Topics: 6212
Posts: 27236

 

Member Stats:
Guest Posters: 49
Members: 31889
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.